如何通过匹配 NSubstitute 中超类的参数来存根方法?

How to stub a method by matching an argument by a superclass in NSubstitute?

参考以下示例代码:

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 存根不匹配。我是否误解了 argument matcher 文档页面,该页面声称 Arg.Any 可以使用 "to match any argument of a specific sub-type"?

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

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