moq/labs

Deep verification of call arguments

Closed this issue · 1 comments

When mocking a service interface, I want to make assertions that a method on the interface was called with a given set of arguments. Currently Moq lets me call Verify on my mock to check, but will only perform equality comparisons on expected and actual arguments using Equals. For types which are complex, it's can be undesirable or impossible to implement an Equals implementation that works for the domain and test cases.

Instead, I'm having to Setup my Moq in a way which captures the arguments so I can make assertions on them after asserting that a call has been made:

Bar actualBar = null;
var service = new Mock<IService>();
service
    .Setup(x => x.Foo(It.IsAny<Bar>())
    .Callback<Bar>(b => actualBar = b);

// ...Run the test...

// Assert the service was called once.
service.Verify(x => x.Foo(It.IsAny<Bar>(), Times.Once);

// Assert the service was given the correct information.
actualBar.ShouldBeEquivalentTo(expectedBar);

Is there some way to get access to the recorded invocations other than using Verify? Could there be a way to extend Verify to perform more complex assertions and report on failures more clearly?