本文介绍了NSubstitute测试本身可以运行,但是会在套件中引发意外的Matcher参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a unit test where I use .Returns() to return some sample data:

    [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运行器在Visual Studio中传递.当它作为列表的一部分运行时会失败,例如当我从解决方案中运行所有测试时.

This code passes in Visual Studio using the ReSharper runner, when the test is run alone. It fails when it runs as part of a list, like when I Run All Tests from Solution.

NSubstitute.Exceptions.UnexpectedArgumentMatcherException:参数匹配器(Arg.Is,Arg.Any)仅应用于代替成员参数.请勿在Return()语句或成员调用之外的任何其他地方使用.

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.

我什至看不到我在哪里使用Arg.Any或Arg.Is.我在做什么使NSubstitute抱怨?当我使用.Returns()返回非本地对象的列表时,就会发生这种情况.

I don't see where I am even using Arg.Any or Arg.Is. What am I doing that makes NSubstitute complain? This happens when I use the .Returns() to return lists of non-native objects.

推荐答案

这最像是由于先前的测试使用了针对非虚拟方法的参数匹配器,或者是在 Returns 语句中进行的测试.

This is most like due to a previous test using an argument matcher against a non-virtual method, or in a Returns statement.

不幸的是,这可能很难调试.第一步是查看在此治具中运行所有测试时是否出现问题.如果是这样,请检查该固定装置中 Arg.Is | Any 的所有使用,从在测试失败之前立即运行的那个开始(如果您的测试框架使用可预测的测试顺序,否则您将需要查看测试日志以查看失败的测试会继续进行.)

Unfortunately this can be quite tricky to debug. First step is to see if the problem occurs when you run all the test in this fixture. If so, check all uses of Arg.Is|Any in that fixture, starting with the one that runs immediately before the test that fails (if your test framework uses a predictable test order, otherwise you'll need to look at test logs to see what tests proceed the failing one).

如果该固定装置没有发生这种情况,则需要查看事先运行的固定装置,以查看剩余的arg匹配器来自何处.它最有可能在测试失败附近.

If it does not occur with that fixture you'll need to look through the fixtures that run beforehand to see where the left over arg matcher is coming from. It is most likely somewhere near the failing test.

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

EDIT 2021-03-28: The NSubstitute.Analyzers package can help to find these issues at compile time. I highly recommend adding it to any test project that includes NSubstitute.

这篇关于NSubstitute测试本身可以运行,但是会在套件中引发意外的Matcher参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 06:24