Search code examples
rhino-mocks

Rhino.Mocks: can AssertWasCalled use a custom IEqualityComparer when comparing the method arguments?


I have the following code snippet:

Action<IProbingErrorHandler> handlerAction = x => x.Post(sourceContext, channelId, probingError);
var handler = MockRepository.GenerateStub<IProbingErrorHandler>();
handler.Stub(handlerAction);
...
handler.AssertWasCalled(handlerAction);

Unfortunately, I had to implement the Equals method in the argument types in order for the last assertion to work. Is there a way to use a custom IEqualityComparer implementation instead?


Solution

  • Try using custom argument constraints. This will let you write something like:

    handler
        .Stub(x => x.Post(Arg<string>.Matches(s => ...), ..., ...)) 
        .Return(something);
    

    Check the Rhino Mocks constraints reference for some of the built in matchers. If none of those do what you need, you can create your own by subclassing AbstractConstraint or one of the existing matchers.