RamblingRoss
The blog of Ross Fruen, a .NET consultant

NUnit mocks and the Extend method

One of the projects I have been working on uses the nunit.mocks assembly.

Previously this project used release 2.4.7 of NUnit. On upgrading to 2.4.8 some tests started to fail.

It appears that the behaviour of the Expect method has subtly changed. Previously the number of times a mocked method was called was irrelevant. With 2.4.8 it now appears Expect needs to be called for each invocation.

The following code under 2.4.7 would allow LogInformation on ILogger to be called any number of times, but under 2.4.8 would only allow it to be called once.


internal static ILogger GetMockLogger()
{
DynamicMock mockLogger = new DynamicMock(typeof(ILogger));
ILogger logger = (ILogger)mockLogger.MockInstance;
mockLogger.Expect("LogInformation");
return logger;
}

To allow log Information to be called twice the following code would be used:


internal static ILogger GetMockLogger()
{
DynamicMock mockLogger = new DynamicMock(typeof(ILogger));
ILogger logger = (ILogger)mockLogger.MockInstance;
mockLogger.Expect("LogInformation");
mockLogger.Expect("LogInformation");
return logger;
}

Add a comment

If you want your comment to appear on this page please complete the form below. Your name and email address are optional, although the latter will be required if you want a response. Your email address will not appear against your comment and will only be used to correspond with yourself (where appropriate).

Thanks!

Thank you for submitting your comment, it will appear here after moderation is complete.

Sorry

There was a problem sending your comment, please try again.