Build/Test Issues

NCrunch 3.13.0.7 gives intermittent UnexpectedArgumentMatcherException errors

Started by rdavis on 3,625 views

After upgrading to NCrunch 3.13.0.7, I'm getting regular occurrences of an UnexpectedArgumentMatcherException. They're occurring at most once per test run, and the exception message is like this:

SetUp : NSubstitute.Exceptions.UnexpectedArgumentMatcherException : Argument matchers (Arg.Is, Arg.Any) should only be used in place of member arguments. Do not use in a Returns() statement or anywhere else outside of a member call.
Correct use:
sub.MyMethod(Arg.Any<string>()).Returns("hi")
Incorrect use:
sub.MyMethod("hi").Returns(Arg.Any<string>())


Re-running the test always clears the exception.

However, none of my tests use Arg.Any inside a Returns method call. An example line that produces this exception is

_mockDateTimeProvider.UtcNow.Returns(_defaultDateTime);

where _mockDateTimeProvider is an object created by AutoFixture, and _defaultDateTime is a static DateTime object.

Is there perhaps something else causing these exceptions, or is this an issue in the 3.13.0.7 version of NCrunch?

Thanks!

Edited

Hi, thanks for sharing this issue.

Does the exception appear with any consistent stack trace? Are you able to get a debugger on it?

(NCrunch itself has no integration with NSubstitute).
Thanks for the reply. I found that this issue was raised on StackOverflow a while ago - https://stackoverflow.com/questions/38283104.

In our case, a couple of tests were using AutoFixture's fixture.Create<T1>() method to build an object, and then passing Arg.Any<T2>() as a parameter in a method call on this object. This causes NSubstitute's static queue to get out-of-sync, as explained well here: https://weareadaptive.com/2014/09/30/why-nsubstitute/

Replacing these parameters with fixture.Create<T2>() fixed the issue.

So this was not a problem with NCrunch, but some change between 3.12.0.15 and 3.13.0.7 made this problem more likely to be visible. But that's a good thing!

Post a reply

Log in to reply.