NSubstitute Error UnexpectedArgumentMatcherException

nsubstitute received async
some argument specifications (e.g. arg is arg any were left over after the last call)
nsubstitute received not working
nsubstitute receivedcalls
nsubstitute isany
nsubstitute arg is null
nsubstitute c#
nsubstitute params

I'm getting the following error:

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()).Returns("hi") Incorrect use: sub.MyMethod("hi").Returns(Arg.Any())'

When trying to mock out the following interface:

public interface IMyDate
{
    DateTime GetDate();
}

Here's where I mock it:

var myDate = Substitute.For<IMyDate>();
myDate.GetDate().Returns(testDate); // Error thrown here

Please can anyone explain what I'm doing wrong?

var myDate = Substitute.For<IMyDate>();
myDate.GetDate().Returns(new DateTime(2018, 04, 05)); // Error thrown here

Gives the same result.


Most likely testDate is an argument matcher (Arg.Is or Arg.Any)

The exception message is telling you just to use argument matchers as member call arguments, not as return values, but you are are using it as return value...

"Do not use in a Returns() statement or anywhere else outside of a member call"

Try

var testDate = new DateTime(); //<-- try setting the desired date as needed
var myDate = Substitute.For<IMyDate>();
myDate.GetDate().Returns(testDate);

Reference NSubstitute: Argument matchers

Erroneous "UnexpectedArgumentMatcherException" being received , Describe the bug We are receiving a UnexpectedArgumentMatcherException when The first thing worth trying is installing the NSubstitute. We are receiving a UnexpectedArgumentMatcherException when running a test which has previously passed repeatedly without any issues. We cannot see any logical reason why this exception is being thrown.


This might be due to a problem with an earlier test. See this answer for some steps for tracking down this issue. I've included a snapshot of that answer below:

This is most like due to a previous test using an argument matcher against a non-virtual method, or in a Returns statement.

Unfortunately this can be quite tricky to debug. First step is to see if the problem occurs when you run all the test in this fixture. If so, check all uses of Arg.Is|Any in that fixture, starting with the one that runs immediately before the test that fails (if your test framework uses a predictable test order, otherwise you'll need to look at test logs to see what tests proceed the failing one).

If it does not occur with that fixture you'll need to look through the fixtures that run beforehand to see where the left over arg matcher is coming from. It is most likely somewhere near the failing test.

Hope this helps. In positive news the next NSubstitute version (v4) will offer more help in these cases.

See also: How not to use argument matchers.

Argument matchers, If you are stuck on an earlier version (getting an error such as CS7085: By- reference return type 'ref T' is not supported while trying to use them) please use � A friendly substitute for .NET mocking libraries. Contribute to nsubstitute/NSubstitute development by creating an account on GitHub.


To fix this issue, you need to identify the method that causes it, this can be the method you're trying to call or another nested method inside the method you're trying to call...

Making that method virtual fixes this issue.

If you substituted for a class rather than an interface, check that the call to your substitute was on a virtual/abstract member. Return values cannot be configured for non-virtual/non-abstract members.

Compatibility argument matchers, This lets them be used with out and ref parameters, but it also means that if you are stuck on an earlier version of C# you may get an error like the following when � NSubstitute.Exceptions.CouldNotSetReturnDueToTypeMismatchException : Can not return value of type ObservableCollection`1 for GlobalSecurityStateViewModel.remove_RefreshingRolesAndUsers (expected type Void).


Upgraded to 1.8 and .Returns throws ArgMatchers exception on , UnexpectedArgumentMatcherException' occurred in NSubstitute.dll but but hopefully you'll get this error immediately if an arg matcher goes� In this case command did receive a call to Execute(), and so will complete successfully.If Execute() has not been received NSubstitute will throw a ReceivedCallsException and let you know what call was expected and with which arguments, as well as listing actual calls to that method and which the arguments differed.


Fixing Intermittent Test Failures With NCrunch, UnexpectedArgumentMatcherException: Argument matchers (Arg.Is, In every case the cause of failure was an incorrect use of NSubstitute� NSubstitute is open source software, licensed under the BSD License. The NSubstitute project is possible thanks to a number of other software projects. We acknowledge their awesomeness. NSubstitute logo donated by Troy Hunt.


NCrunch 3.13.0.7 gives intermittent , UnexpectedArgumentMatcherException : Argument matchers (Arg.Is, ( NCrunch itself has no integration with NSubstitute). but some change between 3.12.0.15 and 3.13.0.7 made this problem more likely to be visible. NSubstitute is designed for Arrange-Act-Assert (AAA) testing, so you just need to arrange how it should work, then assert it received the calls you expected once you're done. Because you've got more important code to write than whether you need a mock or a stub.