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 trying to mock an overloaded function that has multiple arguments.

For example,

Class Code
...

public void OverloadedFunction (string st1, string st2, ref Tree treeObj)
public Plant OverloadedFunction(int index)


Test Code

Mock mockMyClass = MockManager.MockAll(typeof(MyClass));
...
mockMyClass.ExpectAndReturn("OverloadedFunction").Args(somestring1, somestring2, ref someTree);


The TypeMock verification will return me an error with a wrong number of expected parameters.

How can I specify with function the mock should go to?

Notice that I have a ref argument in my function as well.

Thanks,

YFC
asked by YFC (1.2k points)

4 Answers

0 votes
Hi,

Basically you should not need to specify to which method the expectation should go.
I'm still kind of vague on the exact problem that you are encountering, would it be possible to post the entire test code with the exact error that you get?

Reading your post, my hunch is that during the execution of the code the unexpected variant of OverloadedFunction is called. If this is the case,
you will need to explicitly add an expectation on that call as well, if you don't want that call to be mocked use the ExpectUnmockedCall API.
:!: when dealing with overloaded methods the order in which the expectations are added is important and should be the same as the execution order.
answered by lior (13.2k points)
0 votes
Hi,

Thanks for your reply.

I did some more investigation, and I think the problem is, in my overloaded function I actually call the other version of the function.

So let's say I have:

// 1st version
private Tool AddToolToBand(string toolName, int toolID, ref Band aBand)
{
...
}

//2nd version
private Tool AddToolToBand(string toolName, int toolID, ref Band aBand, ToolStyles toolStyle, ToolTypes toolType, string caption, string toolTip, string shorcut, stdole.StdPicture toolPic)
{

     //call to 1st version
     Tool aTool = AddABToolToBand(toolName, toolID, ref aBand);

     //do other stuff
     ...
}


And my test code for the 2nd version of AddToolToBand:

public void AddToolToBandOverloadTest()
{

...

     Tool retTool = null;

     //try to mock the call to the first version
     mockMyClass.ExpectAndReturn("AddToolToBand", retTool).Args("toolName", 1, aBand);

...

     //call the 2nd version under test
     accessor.AddToolToBand([9 arguments]);

}


The error is TypeMock Verification: Call to AddToolToBand() 3 Parameters expected but received 9. When I debug through it, it goes to the right version of the function, but doesn't enter and ends right away.

It is as if TypeMock is trying to mock the function I'm actually trying to test (1st version), because I have mocked the 1st version.

Is TypeMock able to handle this?

Also, as a side question, how do I deal with ref arguments when I want to check for arguments of an expected function (as I would have to do above with Band?)

Thanks,

YFC
answered by YFC (1.2k points)
0 votes
Hi,

I found the solution to my own problem.

I tell TypeMock to "not" mock the version of the function I'm testing using ExpectUnmockedCall(), which is what you actually suggested in your reply.

Thanks!

YFC
answered by YFC (1.2k points)
0 votes
cool. If you need any more help on this let me know

Regarding ref arguments, if you want to check that the passed ref arg has the proper value, just use Args(...) normally.
If you want to mock the value returned from the method on the ref args, use the Assign class.
answered by lior (13.2k points)
...