This is a migrated thread and some comments may be shown as answers.

MustBeCalled partial argument match

1 Answer 38 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Edoardo
Top achievements
Rank 1
Edoardo asked on 16 May 2012, 04:25 PM
Another question on MustBeCalled. Is there a way to assert a partial match of an argument?

For example, the first argument of the call to be asserted is an object which has 10 properties but I only care about 2 of them in the particular test case.

Thanks!

A failed attempt/example:

var accounts = ...
var target = Mock.Create<TargetClass>(Behavior.CallOriginal, accounts);

Mock.Arrange(() => target.TestCall(Arg.IsAny<Account>()).DoNothing();

target.Run(); // Will call internal TestCall for each account

foreach(var account in accounts)
Mock.Assert(() => target.TestCall(Arg.Matches<Account>(x => x.FirstName == account.FirstName && x.LastName == account.LastName), Occurs.Once());

Error is: Expected TargetClass.TestCall(Accoutn) call on the mock should be once, but it was called 0 times(s).

Any suggestion, please?

1 Answer, 1 is accepted

Sort by
0
Ricky
Telerik team
answered on 21 May 2012, 04:33 PM
Hi Edoardo,

Thanks again for contacting us.

However, Mock.Assert will fail right away when something that is expected but not executed. Therefore, in your case asserting specific criteria in a loop won't work as the moment it will hit with the wrong expectation the test will fail.

Kind Regards
Ricky
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

Tags
General Discussions
Asked by
Edoardo
Top achievements
Rank 1
Answers by
Ricky
Telerik team
Share this question
or