chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
Welcome to Typemock Community! Here you can ask and receive answers from other community members. If you liked or disliked an answer or thread: react with an up- or downvote.
0 votes
So the set up is this, I have a class and methods

Class DoThings
getCurrentThing returns Thing
string userName = WindowsIdentity.GetCurrent().Name;
call lookupThing(userName)

Class UseDoThings
lastThingDone() returns the string we want to check
call DoThings.getCurrentThing

So we have created the following test code

Isolate.WhenCalled(() => new DoThings()).ReturnRecursiveFake(); //not sure it is required

DoThings fakeDoThings = new DoThings();

Isolate.WhenCalled(() => new DoThings()).WillReturn(fakeDoThings);
var fakeThing = new Thing ("Ogedei", "Sechen", "", "Ogedei.Sechen@Mongols.horde");
Isolate.WhenCalled(() => fakeADHelper.GetCurrent()).WillReturn(fakeThing);
Isolate.NonPublic.WhenCalled(fakeADHelper, "lookupUser").WillReturn(fakeUser);
These lines do not cause the target call to return the fakeUser only the current user

Isolate.WhenCalled(() => WindowsIdentity.GetCurrent().Name).WillReturn("UserName");
this tosses an error

Error Message
Test method ServicesTests.UtilityServiceTest.LastLoginInfoTest threw exception:
*** No method calls found in recording block. Please check:
* Are you trying to fake a field instead of a property?
* Are you are trying to fake an unsupported mscorlib type? See supported types here:

Error Stack Trace
gg.a(cx A_0, Boolean A_1)
bh.a(Boolean A_0)
dl.b(Boolean A_0)
ij.b(Boolean A_0)
ij.a(Object A_0, Boolean A_1, Func`1 A_2, Action A_3, Action A_4, Action A_5, Boolean A_6)
ij.e(Object A_0)
TypeMock.ArrangeActAssert.ExpectationEngine`1.a(TResult A_0)

UtilityService target = new UtilityService(); // TODO: Initialize to an appropriate value
UtilityService_Accessor targetAcc = new UtilityService_Accessor(); // TODO: Initialize to an appropriate value

string actual = target.LastLoginInfo();

string targetValue = String.Format("User <i>{0}</i> last logged on {1} at {2}", "Brian.Jones@EMG.Local", "", "") +
String.Format("<br>Bad Password Time: {0}<br>Last Successful Interactive Logon: {1} " +
"<br>Last failed logon time", "", "", "") +
String.Format("<br>Number of unsuccessful logon attempts: {0}<br>Failed Interactive Logon Count At Last Successful Logon: {1} "
, "", "");

target = new UseDoThings
actual = target.LastThingDone()

this always returns the current user and not the mocked value.

I am not sure what I am missing in the faking that is not overriding the call and what is wrong with the Windows Identity call as well

Thanks in advance for your assistance
asked by OgedeiSechen (3.1k points)

2 Answers

0 votes

We have a special API just for faking instances.
You could use it like this :
Let's say this is your code under test:
    public class Foo
        public int GetNum()
            var dependency = new Dependency();
            return dependency.GetNum();

    public class Dependency
        public int GetNum()
            return 10;

You can use Isolate.Fake.AllInstances that will fake every instance you will create.
        public void TestMethod1()
            var fakeDependency = Isolate.Fake.AllInstances<Dependency>();
            Isolate.WhenCalled(()=> fakeDependency.GetNum()).WillReturn(3);

            var foo = new Foo();
            var num = foo.GetNum();


You can use Isolate.Fake.NextInstance which fakes the the first instance you will create.
        public void TestMethod2()
            var fakeDependency = Isolate.Fake.NextInstance<Dependency>();
            Isolate.WhenCalled(() => fakeDependency.GetNum()).WillReturn(3);

            var foo = new Foo();
            var num = foo.GetNum();

            Assert.AreEqual(3, num);

Let me know if it helps you.
answered by Shai Barak (1.5k points)
0 votes
Thank you that helped us.
answered by OgedeiSechen (3.1k points)