Search code examples
c#nmock

How can I use an NMock3 Method Parameter in a "Will" Action


I'm attempting to mock a method that takes an exception as a parameter and under certain circumstances may throw that exception. Is there a way to mock this such that the mocked method does that?

I'm figuring a syntax something like below.

myMock.Expects.One.Method(mo => mo.ProcessException(null)).With(<capture exception>).Will(new ThrowAction(<captured exception>));

Is there a way to do this? In this situation I actually know what type of exception I'm expecting, so in theory I could reconstruct it, but it seems better to throw the called exception.


Solution

  • You need to use Collect.MethodArgument

    mockPostalCodeChecker.Expects.One.Method(m => m.ProcessException(null)).With(<what you want>).Will(Collect.MethodArgument<Exception>(0, delegate(Exception excep) { throw excep; }));