NSubstitute 测试本身有效,但会在套件中抛出意外的匹配器参数

NSubstitute test works by itself, but throws Unexpected Matcher Argument in a suite

我有一个单元测试,我使用 .Returns() 来 return 一些示例数据:

    [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);
    }

当测试单独 运行 时,此代码使用 ReSharper 运行ner 在 Visual Studio 中传递。当它 运行 作为列表的一部分时它会失败,就像我 运行 来自解决方案的所有测试。

NSubstitute.Exceptions.UnexpectedArgumentMatcherException:参数匹配器(Arg.Is、Arg.Any)应该只用于代替成员参数。不要在 Returns() 语句或成员调用之外的任何其他地方使用。

我什至没有看到我在哪里使用 Arg.Any 或 Arg.Is。我在做什么让 NSubstitute 抱怨?当我使用 .Returns() 到 return 非本地对象列表时会发生这种情况。

这很可能是由于先前针对非虚拟方法或在 Returns 语句中使用参数匹配器进行的测试。

不幸的是,这可能很难调试。第一步是查看当您 运行 在此夹具中进行所有测试时是否出现问题。如果是这样,请检查该夹具中对 Arg.Is|Any 的所有使用,从测试失败前 运行 的那个开始(如果您的测试框架使用可预测的测试顺序,否则您将需要查看测试日志以查看失败的测试进行了哪些测试)。

如果该灯具没有发生,您需要事先查看 运行 的灯具,以查看剩余的 arg 匹配器来自何处。它很可能在失败测试附近的某个地方。

编辑 2021-03-28:NSubstitute.Analyzers 包可以帮助在编译时发现这些问题。我强烈建议将它添加到任何包含 NSubstitute 的测试项目中。

在我的例子中,它是对扩展方法的 Received() 调用(可能是因为它不是虚拟的)。 所以我回到我的拉取请求并删除了它的每个实例并且它起作用了。