April 18, 2013

RhinoMocks and Use Arg T only within a mock method call while recording exception

Ouch, pained my brain over this one for the last half hour or so, but finally found the solution.

I had a call similar to:

    .Stub(x => x.TryGet(<Specification>.Matches(y => y.Something), Arg<Customer>.Out(customerToReturn).Dummy))

Because my first argument had a fairly large Matches call (it’s simplified here), I refactored it to:

var specification = Arg<Specification>.Matches(y => y.Something);
    .Stub(x => x.TryGet(specification, out Arg<Customer>.Out(customerToReturn).Dummy))

Ah, much more readable! Only, it didn’t work. The exception I got was:

Use Arg<T> ONLY within a mock method call while recording. 2 arguments expected, 3 have been defined.

I could not for the life of me see where I was defining three arguments. I was looking at the second argument (the out argument) because that was the more exotic of the two.

I finally realized the exception message is actually rather correct. You have to use Arg inside the method you’re stubbing. So you can’t refactor like I did. I had to leave the call as it was:

    .Stub(x => x.TryGet(Arg<Specification>.Matches(y => y.Something), out Arg<Customer>.Out(customerToReturn).Dummy))

I hope this saves you (and me!) some time in the future (as the few posts I found focus on the fact that the method has to be virtual, which doesn’t help if you’re stubbing an interface).

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.