0

我有一个示例代码(粘贴在下面)。我正在使用 Xunit、NSubstitute 和 NCrunch。当我在 Visual Studio 的测试资源管理器中运行测试时,测试通过了。当我调试时,测试运行正常。使用 NCrunch,我面临着一种奇怪的行为,测试通过,然后失败,然后通过,然后失败,并继续这样。

[Fact]
public void Test()
{
    var drink = Substitute.For<IDrink>();
    var greetings = new Greetings();

    var derived = new Derived();
    derived.Test(drink, greetings);
}

public class Greetings
{
    public string Message { get; set; }
}

public interface IDrink
{
    void Prepare(Greetings greetings);
}

public abstract class Base
{
    public abstract void Test(IDrink drink, Greetings greetings);
}

public class Derived : Base
{
    public override void Test(IDrink drink, Greetings greetings)
    {
        drink.Prepare(greetings); /////////// The error is here
    }
}

public class NullDerived : Derived
{
    public override void Test(IDrink drink, Greetings greetings)
    {
        throw new Exception("No value found");
    }
}

NCrunch 抛出的错误是:

NSubstitute.Exceptions.RedundantArgumentMatcherException: Some argument specifications (e.g. Arg.Is, Arg.Any) were left over after the last call.

This is often caused by using an argument spec with a call to a member NSubstitute does not handle (such as a non-virtual member or a call to an instance which is not a substitute), or for a purpose other than specifying a call (such as using an arg spec as a return value). For example:....

我试图从 Prepare(Greetings greetings) 方法中删除参数,然后 NCrunch 每次都通过测试。

正如错误所暗示的那样,我没有正确传递测试中的参数。

我的问题是:将问候对象传递给 Prepare 方法的正确方法是什么。我试过 Arg.Any 但它没有用。

任何帮助表示赞赏。

编辑 1: 我使用 Reshaper 的单元测试覆盖率看到了相同的行为。

4

0 回答 0