Suppose I have the following class:
public class TestBase { public bool runMethod1 { get; set; } public void BaseMethod() { if (runMethod1) ChildMethod1(); else ChildMethod2(); } protected abstract void ChildMethod1(); protected abstract void ChildMethod2(); }
I also have the class
public class ChildTest : TestBase { protected override void ChildMethod1() { //do something } protected override void ChildMethod2() { //do something completely different } }
I’m using Moq, and I’d like to write a test that verifies ChildMethod1() is being called when I call BaseMethod() and runMethod1 is true. Is it possible to create an implemention of TestBase with Moq, call BaseMethod() and verify that ChildMethod was called on the Moq implementation?
[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 }
You can also set the expectation/setup as Verifiable and do without a strict mock:
Edit This syntax does not work in current versions of Moq. See this question for how to do it as of at least 4.0.10827