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 #587

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

Comments

@DavidKlempfner
Copy link

DavidKlempfner commented Aug 29, 2019

Describe the bug
NSubstitute.Exceptions.UnexpectedArgumentMatcherException should be thrown for a test, however it is only thrown when the test is run in succession after another test.

To Reproduce
Run A() and B() by themselves, both will pass.
Run A() and B() by right clicking in the .cs file and selecting "Run all tests" -> only A() will pass.

using NSubstitute;
using NUnit.Framework;
using System;

namespace Tests
{
    [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;
    }

    public class Creditor
    {
        public Guid CompanyExternalId { get; set; }
    }
}

Expected behaviour
Both tests should throw the Exception, regardless of whether or not they were run individually, or as part of the "Run all tests in file".

Environment:

  • NSubstitute version: [4.2.1]
  • NSubstitute.Analyzers version: [CSharp 1.0.1.0]
  • Platform: [.NET Core 2.2, Windows 10]
@dtchepak
Copy link
Member

dtchepak commented Aug 29, 2019

Hi @backwardsDave1 ,

Thanks for the concise repro case.

I don't think we can detect this case at run-time. We are going to try to detect this statically with NSubstitute.Analyzers (issue 35). Please follow that issue for progress updates.

Regards,
David

@304NotModified
Copy link
Contributor

As nsubstitute/NSubstitute.Analyzers#35 has been fixed, I will close this one

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

No branches or pull requests

3 participants