I have these two tests (stripped to the bare bones to replicate the error):
[TestFixture]
public class CreditorMapperTests
{
private IAbcContext _AbcContext;
[SetUp]
public void Setup()
{
_AbcContext = Substitute.For<IAbcContext>();
_AbcContext.CompanyInfo.Returns(x => new CompanyInfo(Arg.Any<Guid>()));
}
[Test]
public void A()
{
Creditor publishDocument = new Creditor();
publishDocument.CompanyExternalId = _AbcContext.CompanyInfo.UniqueId;
}
[Test]
public void B()
{
Creditor publishDocument = new Creditor();
publishDocument.CompanyExternalId = _AbcContext.CompanyInfo.UniqueId;
}
}
public interface IAbcContext
{
CompanyInfo CompanyInfo { get; }
}
public class CompanyInfo
{
public CompanyInfo(Guid uniqueId)
{
UniqueId = uniqueId;
}
public readonly Guid UniqueId;
}
The Setup() for A() runs fine. However when Setup() is called for B(), I get this 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())
This only happens when I run both tests by running all tests in that class.
If I run B() by itself, the Exception is not thrown.
Why does Setup() for B() fail only when run automatically after A()?
(nb. both tests are identical).
I'm using NUnit v3.8.1 and NSubstitute v2.0.3
This is an error message from NSubstitute. The message itself explains what is wrong with your code. There is nothing NUnit can do about it.
If you are asking why your code only throws the second time and not the first time, you're asking the wrong people. You could ask the developer of NSubstitute, but perhaps a better use of your time is to stop using Arg.Any() in a way it is not intended to be used.
I've submitted an issue here:
https://github.com/nsubstitute/NSubstitute/issues/587
@backwardsDave1 I'm closing this issue, as this has nothing to do with NUnit.
Most helpful comment
This is an error message from NSubstitute. The message itself explains what is wrong with your code. There is nothing NUnit can do about it.
If you are asking why your code only throws the second time and not the first time, you're asking the wrong people. You could ask the developer of NSubstitute, but perhaps a better use of your time is to stop using Arg.Any() in a way it is not intended to be used.