MustBeCalled partial argument match

2 posts, 0 answers
  1. Edoardo
    Edoardo avatar
    5 posts
    Member since:
    Mar 2011

    Posted 16 May 2012 Link to this post

    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.


    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?
  2. Ricky
    Ricky avatar
    467 posts

    Posted 21 May 2012 Link to this post

    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
    the Telerik team

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

  3. DevCraft R3 2016 release webinar banner
Back to Top