1

参考以下示例代码:

using NSubstitute;
using NUnit.Framework;

public class Class1
{
}

public class Class2
{
    public void Method(Class1 class1)
    {
    }
}

public class Class3 : Class1
{
}

[TestFixture]
public class ArgAnyTest
{
    [Test]
    public void Test()
    {
        var called = false;
        var class2 = Substitute.For<Class2>();
        class2.When(@this => @this.Method(Arg.Any<Class1>())).Do(invocation => called = true);

        class2.Method(new Class3());

        Assert.That(called, Is.EqualTo(true));
    }
}

断言失败,表明Method存根不匹配。我是否误解了声称可用于“匹配特定子类型的任何参数”的参数匹配器文档页面?Arg.Any

4

1 回答 1

2

看来问题不在于参数匹配,而在于存根。 Method必须是虚拟的,否则它不会被存根:

public class Class2
{
    virtual public void Method(Class1 class1)
    {
    }
}
于 2018-01-09T03:31:01.577 回答