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
Hi,
I am using TypeMock Isolator 5.3 for mocking out private methods in my unit test. At a higher-level, calling "IgnoreCall" on a particular definition of a method that has multiple definitions does not work. TypeMock acts as if there is no "IgnoreCall" specified for that method.

Let me dive into a little bit of detail here -

Lets say I am unit-testing the following class:
public ClassA {
...
...

private static Sum(int x, int y){
    --- implementation ---
}

private static Sum(Inputs inputs){
    Sum(inputs.x, inputs.y)
}
...
...

}


The goal is here to verify that Sum(x, y) is called with inputs.x & inputs.y when I call Sum(inputs).

So my unit-test code looks somewhat like this -

[Test, Isolated]
public void Test_Sum_WithInputs() {

   Isolate.NonPublic.WhenCalled(typeof(ClassA), "Sum").WithGenericArguments(typeof(int), typeof(int)).IgnoreCall();
   Inputs inputs = new Inputs(2, 3);

   // Use reflection to call the private static method
   typeof(ClassA).RunStaticMethod("Sum", inputs);


   Isolate.Verify.NonPublic.WasCalled(typeof(ClassA), "Sum").WithArguments(2, 3);
}



What happens when I run the test with above code?

TypeMock complains about some missing dependency in the method Sum(int x, int y). But I asked it to Ignore it, instead it would call it.

Is there a different way of unit-testing situations like this? Am I doing something wrong here?

Essentially I am trying to learn how TypeMock isolator behaves when I am dealing with overloaded methods irrespective of whether a public method is calling a public method or a private method or any other combination you can think of.

Any inputs are greatly appreciated,
Thanks,
Sudhir
asked by darbha (680 points)

4 Answers

0 votes
Hi Sudhir

The error about missing dependency is because you are using WithGenericArguments(...) method on non generic method.

When faking private methods the Isolator can not handle overload so all methods with the name Sum are faked.


:!: Faking private methods should be used with caution. Since you are testing internal behavior of the class, the test will break easily with any small change that will change the internal behavior without really changing the results.
answered by ohad (35.5k points)
0 votes
So what you are saying is - using Isolator I cannot unit-test overloaded private methods effectively?
answered by darbha (680 points)
0 votes
Hi,

Faking the behavior of the private overloaded method is limited. I'll add it as a feature request to Isolator.

One workaround i can think of is renaming one of the private methods.

By the way, you can use Isolator to Invoke a private method using AAA API:
Inputs inputs = new Inputs(2, 3);
Isolate.Invoke.Method(typeof (ClassA), "Sum", inputs);


If you wish, you can post more of the code and I'll help writing a test based on state instead of interaction if possible.

Best Regards,
Elisha
Typemock Support
answered by Elisha (12k points)
0 votes
Thanks for the suggestions Elisha. I am good for now, but would like to hear when the feature gets added to the API. Thanks again.
answered by darbha (680 points)
...