简体   繁体   中英

print GetArgumentsForCallsMadeOne if assert failed in RhinoMock

Let's say I have a test using RhinoMock. I'm using the AAA aproach, so it's usually ended with fooMock.AssertWasCalled(x=>x.Foo(bar))

If the assertion failed, I'd usually add a line looking like this fooMock.GetArgumentsForCallsMadeOn(x=>x.Foo(null)).PrintDump()

so I can see what calls was made on mock (it helps, because usually the problem is that mock is called with wrong arguments).

Is there any way I can automate the process? So, tell RhinoMock to print the calls was made on mock's method if an assertion failed?

It seems there is no built-in mechanism to dump arguments for failed assertions.

I'd suggest to use an extension method like the following:

public static void AssertWasCalledAndDump<T>(this T self, Action<T> action)
{
    try
    {
        self.AssertWasCalled(action);
    }
    catch (Rhino.Mocks.Exceptions.ExpectationViolationException)
    {
        self
            .GetArgumentsForCallsMadeOn(action, options => options.IgnoreArguments())
            .PrintDump();
        throw;
    }
}

Then in the code you can just write:

fooMock.AssertWasCalledAndDump(x=>x.Foo(bar));

PS
I assume you already have an implementation of PrintDump() extension.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM