NSubstitute
NSubstitute copied to clipboard
UnexpectedArgumentMatcherException when calling Setup second time
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]
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