Click here to Skip to main content
15,881,852 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more: , +
Hi


I am trying to figure out a way to test if a method is called or not in one of my test case and I might use some expertise as i am new to fakes. First let me try to explain my problem

ok so i have a method like below one

public static void Mthod1 (parm1, parm2, parm3)
{

// call to another method method2

method2(parm1);

if(parm2 != null)
{
return;
}

method3(parm3);

}


now i am writing unit test using fakes where i am shiming method2 and method3.if parm2 is null method return and method3 is not called. my problem is how to prove in my unit test that method3 is not called.

Thanks in advance.
Posted
Comments
Richard C Bishop 20-May-13 17:50pm    
The code you have provided will not compile. You do not declare datatypes in your method arguements and your method returns nothing but yet you have a "return" in its scope. What do you mean by proving that "method3" is not called. If you don't call it, you don't call it.

1 solution

If you need to know how many times an external interface method is called, use some mocking framework. See also What C# mocking framework to use?[^].
These frameworks provide means to assert how many times and with what parameters you expect the method to be called.

If the method is an interal method of the Unit-Under-Test (UUT), then you need to add some internal test code.

In any case, your main challenge will be the decopling of the dependencies. See Inversion of Control[^] and more specifically e.g. Dependency Injection[^] so that you can plug the mocked external code to the UUT and maybe plug in some internal test code to the UUT.

Cheers
Andi
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900