我有一个单元测试,我使用.Returns()返回一些示例数据:
[TestMethod]
public void TestRetrieveElementsInVersion()
{
IRetrieveElementSequence component = Substitute.For<IRetrieveElementSequence>();
List<UnconstructedElement> list = new List<UnconstructedElement>
{
new UnconstructedElement{Version = "1"},
new UnconstructedElement{Version = "2"}
};
component.RetrieveElements().Returns(list); // exception reported here
const string target = "1";
IRetrieveElementSequence service = new RetrieveElementsInAVersion(component, target);
IList<UnconstructedElement> result = service.RetrieveElements();
bool check = result.All(e => e.Version == target);
Assert.IsTrue(check);
}
Run Code Online (Sandbox Code Playgroud)
当测试单独运行时,此代码使用ReSharper运行程序在Visual Studio中传递.当它作为列表的一部分运行时失败,就像我从解决方案运行所有测试一样.
NSubstitute.Exceptions.UnexpectedArgumentMatcherException:参数匹配器(Arg.Is,Arg.Any)只应用于代替成员参数.不要在Returns()语句或成员调用之外的任何其他地方使用.
我不知道我在哪里使用Arg.Any或Arg.Is. 我在做什么让NSubstitute抱怨?当我使用.Returns()返回非本机对象列表时会发生这种情况.
Dav*_*pak 10
这最像是由于先前使用参数匹配器对非虚方法或Returns语句进行的测试.
不幸的是,这可能是非常棘手的调试.第一步是查看在此夹具中运行所有测试时是否出现问题.如果是这样,请检查Arg.Is|Any该fixture中的所有用法,从在测试失败之前运行的那个开始(如果您的测试框架使用可预测的测试顺序,否则您将需要查看测试日志以查看测试进行的测试失败的一个).
如果没有使用该灯具,您需要查看预先运行的灯具,以查看左侧arg匹配器的来源.它很可能是在失败测试附近的某个地方.