参考以下示例代码:
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
来匹配特定子类型的任何参数“?
答案 0 :(得分:2)
似乎问题不在于参数匹配,而在于存根。 Method
必须是虚拟的,否则不会被删除:
public class Class2
{
virtual public void Method(Class1 class1)
{
}
}