Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UnexpectedArgumentMatcherException when calling Setup second time #3361

Closed
DavidKlempfner opened this issue Aug 29, 2019 · 3 comments
Closed

Comments

@DavidKlempfner
Copy link

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

@CharliePoole
Copy link
Contributor

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.

@DavidKlempfner
Copy link
Author

I've submitted an issue here:
nsubstitute/NSubstitute#587

@mikkelbu
Copy link
Member

@backwardsDave1 I'm closing this issue, as this has nothing to do with NUnit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants