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
Hello , This is my first query. I am facing the problem of faking instance.
I have a public method. I want to fake it. I have created the fake object stil it does not work. Please see the code.
--Class Code---
public class Class1
{

public string GetData(string filter)
{
if (filter == "1")
return "1111";
else
return "2222";
}
}
--Class Code---

-----CODE------


[TestMethod(), Isolated]
TESTMETHOD
{
//arrange
Class1 obj = Isolate.Fake.Instance<Class1>();
Isolate.WhenCalled(() => obj.GetData("1")).WillReturn("1111");

//act
actual = target.Index() as ViewResult;

//assert
Isolate.Verify.WasCalledWithAnyArguments(() => obj.GetData("2"));

}

-----CODE------

Verify fails here. That means tough i have mocked it , it is calling real method.
HOw do I solve this?
asked by sudhir (3.5k points)

3 Answers

0 votes
I got one break through.
I have added following code lines in test . This worked.

Isolate.Swap.NextInstance<Class1>().With(obj);

Still not sure whether it is right method to do this or there is any alternative to this.
answered by sudhir (3.5k points)
0 votes
Hi,

From the code you posted it seems that you did the right thing when you used the swapped instances :)

When you use Isolate.Swap.NextInstance you are swapping the next created instance of Class1 (next call to new Class1()) with the faked instance you created with Isolate.Fake<>.
answered by ohad (35.4k points)
0 votes
Thanks for clearification. :-)
answered by sudhir (3.5k points)
...