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 have just had a look in the documentation but could not find a sample of how to use ParameterCheckerEx and CustomChecker when methods have more than one parameter. I tried to use object[] as parameter to CustomChecker but typemock says that a method was called with 2 parameters but only one was expected.
A sample would be very helpful as standard checking does not work in my case. TypeMock says:
MenuActionTest.CopyXmlDocumentToClipboard_Test : TypeMock.VerifyException : 
TypeMock Verification: Call to System.Windows.Forms.Clipboard.SetDataObject() Parameter: 1
    expected: [System.Windows.Forms.DataObject]
     but was: [System.Windows.Forms.DataObject]


Again thanks for your untiring help.

Michael
asked by mknaup (8.5k points)

6 Answers

0 votes
Hi Michael,


I have just had a look in the documentation but could not find a sample of how to use ParameterCheckerEx and CustomChecker when methods have more than one parameter.


In the Args, you have a place for each parameter. For example, if you are checking that the first parameter is "typemock" and the second is anything, you could do the following:
mock.ExpectCall("Method").Args("typemock",Check.IsAny());


if you want to check the first using a ParameterCheckerEx and the second can be any do:
mock.ExpectCall("Method").Args(new ParameterCheckerEx (Checker),Check.IsAny());


Hope this helps
answered by scott (32k points)
0 votes
Hi Scott,

In the Args, you have a place for each parameter.

O dear, I could have found out myself. Sorry!

if you want to check the first using a ParameterCheckerEx and the second can be any do:
mock.ExpectCall("Method").Args(new ParameterCheckerEx (Checker),Check.IsAny());



I think in my case it should be like this:
mock.ExpectCall("Method").Args(Check.CustomChecker(new ParameterCheckerEx(Checker), Document),
      Check.CustomChecker(new ParameterCheckerEx(Checker), DataObject));


ParameterCheckerEventArgs.ArgumentPosition() can be used to look up which parameter is to be checked (in event handler Checker())

Hope this helps

That really helped, thanks a lot! :D

Michael
answered by mknaup (8.5k points)
0 votes
Hi Scott,

now I have a problem with a method that has params as an argument:
public void DeleteRow(string table, params WhereCondition[] conditions)

I tried this:
         this.databaseAccessMock.ExpectCall("DeleteRow").Args("Components", new ParameterCheckerEx(this.DeleteParameterChecker));
...
private bool DeleteParameterChecker(ParameterCheckerEventArgs e)
{
   return true;
}


but the following message is thrown:
TestCase 'Orga.Validator.Database.ComponentDescriptionTest.Delete_Test' failed: TypeMock.VerifyException : Error in argument checker
  ----> System.InvalidCastException : Specified cast is not valid.
   at TypeMock.MockManager.getReturn(String name, String methName, Object context, Object[] par)
   at TypeMock.InternalMockManager.getReturn(Object that, String typeName, String methodName, Object p1, Object p2)

What am I doing wrong here?

Thanks for any help in advance,
answered by mknaup (8.5k points)
0 votes
Hi Michael,
The error "Error in argument checker" is from an error thrown within the delegate (the DeleteParameterChecker method). If you surround your code in a try/catch you will be able to see what errors are throw from within the delegate. This normally happens if the arguments are casted without checking first.

:arrow: You also might want to know that methods ARE NOT mocked when inside the delegate method.

I have actually tried the code you posted, and it works like a charm. Although I might be missing something.

Also tell me if the WhereCondition ia a class, struct or enum?
answered by scott (32k points)
0 votes
Hi Scott,

The error "Error in argument checker" is from an error thrown within the delegate (the DeleteParameterChecker method).

Thanks a lot for that hint. I could debug and found the invalid cast.

:arrow: You also might want to know that methods ARE NOT mocked when inside the delegate method.

An important information :!:

:?: Is or isn't it a good idea to put any Assert.xxx inside the DeleteParameterChecker method?

I have actually tried the code you posted, and it works like a charm. Although I might be missing something.
Also tell me if the WhereCondition ia a class, struct or enum?

The problem was inside the implementation of the DeleteParameterChecker method. By the way, WhereCondition is a class.

Thanks again for your quick help! :D
answered by mknaup (8.5k points)
0 votes
Hi Michael,
:?: Is or isn't it a good idea to put any Assert.xxx inside the DeleteParameterChecker method?

It is actually NOT a good idea to assert inside the Delegte, you will lose many of the services of TypeMock if you do so. (MulitThread Support etc).
If you want to use this technique, you can use the DynamicReturnValue - It can be used for void methods too (it will just ignore the return value).
answered by scott (32k points)
...