Search code examples
c#unit-testingmockingjustmock

JustMock - check value of passed method argument


I use JustMock framework and have the following assertion:

Mock.Assert(() => activityListenerMock.PeriodPassed(
  Arg.Matches<Period>(e => e.Length == expectedLength)));

It fails with cryptic message:

Occurrence expectation failed. Expected at least 1 call. Calls so far: 0

How can I get better message. I want to know with what value it was called.

Method is actually called but with wrong argument because when I change assertion to following it passes:

Mock.Assert(() => activityListenerMock.PeriodPassed(
  Arg.IsAny<Period>()));

Solution

  • One way to see what argument was passed to PeriodPassed is to use JustMock's DebugView

    Place DebugView.IsTraceEnabled = true; at the beginning of the test and add DebugView.CurrentState to the watch. Towards the end you will see something to the tune of this: Invocations: (ByRef ...).PeriodPassed("period value will go here") called 1 time; (signature: ...)

    The period value will be shown in the Invocations list.

    Another way to do this, is to extract the matcher into a separate lambda and use a breakpoint: Predicate<Period> matcher = e => e.Length == expectedLength; Mock.Assert(() => activityListenerMock.PeriodPassed( Arg.Matches<Period>(e => matcher(e))));

    Now you can place a breakpoint inside the predicate and check the value of the e argument. This works because now the predicate is not an expression but an actual function, so now you can debug it.