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
0 votes
I am a Isolate novice, having some trouble with faking a private method that takes an argument. Any help with this would be appreciated.

Method to be faked :

private void MethodToBeCalled(Type t)
{
     // implementation here
}


This is what I have tried, please tell me if I need to bury my head in the sand or am on the right track...

Object o = Isolate.Fake.Instance<Class1>();
Isolate.Swap.NextInstance<Class1>().With((Class1)o);
Isolate.NonPublic.WhenCalled((Class1) o, "MethodToBeCalled");
Isolate.Verify.NonPublic.WasCalled((Class1) o, "MethodToBeCalled");


This test fails, and the reason given is that the method was not called.
Any ideas?


Thanks.
asked by kmenezes (640 points)

4 Answers

0 votes
Hi,

basically you seems to be on the right track.
however it seems that this is not your entire test so it might help to see the rest of the test.

the important part is that the actual call to MethodToBeCalled, is done sometimes between the behavior setting (usage of WhenCalled) and the verification
answered by error (6.6k points)
0 votes
Lior is right (thanks!), the actual call to the code under test is missing. This means you are missing the second A of the AAA (Arrange-Act-Assert) format.
- In the Arrange part you set up test data and define how it will behave (including any faking and whencalled statements you need)
- In the Act part you call the code under test
- In the Assert part you verify that the expected result has been received. This includes any Assert or Isolate.Verify statements.

Doron
Typemock Support
answered by doron (17.2k points)
0 votes
Thank you for your replies.

Question on the ACT part of the unit test :

The "MethodToBeCalled" is a private method that takes as a parameter, an object that would need to be sent in. How do I mock that, or can I?

For e.g. the following is the signature,

private void MethodToBeCalled(MsgObject m)
{
}


Thanks.
answered by kmenezes (640 points)
0 votes
You can call a public method that wraps the private method. Another way is to use private accessors, depending on your unit test framework. The mstest how-to can be found here: http://msdn.microsoft.com/en-us/library/ms184807(VS.80).aspx. Finally, there's the option of obtaining and invoking the method through Reflection.

Another option is to wait until early next week - a feature we are releasing in the next Isolator version is a private invocation API which will let you easily specify the private method and arguments.

Thanks,
Doron
Typemock Support
answered by doron (17.2k points)
...