gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-30 00:18:13 25 4
gpt4 key购买 nike

我有一个单元测试,我使用 .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);
}

当单独运行测试时,此代码使用 ReSharper 运行器在 Visual Studio 中通过。当它作为列表的一部分运行时失败,例如我从解决方案运行所有测试时。

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

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

最佳答案

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

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

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

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

关于c# - NSubstitute 测试本身有效,但会在套件中抛出意外的匹配器参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40922774/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com