假设我有以下课程:
public class TestBase
{
public bool runMethod1 { get; set; }
public void BaseMethod()
{
if (runMethod1)
ChildMethod1();
else
ChildMethod2();
}
protected abstract void ChildMethod1();
protected abstract void ChildMethod2();
}
我也有课
public class ChildTest : TestBase
{
protected override void ChildMethod1()
{
//do something
}
protected override void ChildMethod2()
{
//do something completely different
}
}
我正在使用 Moq,我想编写一个测试来验证当我调用 BaseMethod() 并且 runMethod1 为 true 时正在调用 ChildMethod1()。是否可以使用 Moq 创建 TestBase 的实现,调用 BaseMethod() 并验证在 Moq 实现上调用了 ChildMethod?
[Test]
public BaseMethod_should_call_correct_child_method()
{
TestBase testBase;
//todo: get a mock of TestBase into testBase variable
testBase.runMethod1 = true;
testBase.BaseMethod();
//todo: verify that ChildMethod1() was called
}