gpt4 book ai didi

c# - NSubstitute 错误 UnexpectedArgumentMatcherException

转载 作者:行者123 更新时间:2023-11-30 21:34:57 29 4
gpt4 key购买 nike

我收到以下错误:

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. Correct use:
sub.MyMethod(Arg.Any()).Returns("hi") Incorrect use:
sub.MyMethod("hi").Returns(Arg.Any())'

当尝试模拟出以下界面时:

public interface IMyDate
{
DateTime GetDate();
}

这是我 mock 它的地方:

var myDate = Substitute.For<IMyDate>();
myDate.GetDate().Returns(testDate); // Error thrown here

谁能解释一下我做错了什么?

var myDate = Substitute.For<IMyDate>();
myDate.GetDate().Returns(new DateTime(2018, 04, 05)); // Error thrown here

给出相同的结果。

最佳答案

这可能是由于较早的测试存在问题。参见 this answer有关跟踪此问题的一些步骤。我在下面包含了该答案的快照:

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

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).

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.

希望这对您有所帮助。好消息是,下一个 NSubstitute 版本 (v4) 将在这些情况下提供更多帮助。

另请参阅:How not to use argument matchers .

关于c# - NSubstitute 错误 UnexpectedArgumentMatcherException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49669935/

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