Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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
- }
- }
- [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
- }
- //expect that ChildMethod1() will be called once. (it's protected)
- testBaseMock.Protected().Expect("ChildMethod1")
- .AtMostOnce()
- .Verifiable();
- ...
- //make sure the method was called
- testBase.Verify();
- [Test]
- public BaseMethod_should_call_correct_child_method()
- {
- //strict mocks will make sure all expectations are met
- var testBaseMock = new Mock<TestBase>(MockBehavior.Strict);
- //expect that ChildMethod1() will be called once. (it's protected)
- testBaseMock.Protected().Expect("ChildMethod1")
- .AtMostOnce();
- var testBase = testBaseMock.Object;
- testBase.runMethod1 = true;
- testBase.BaseMethod();
- //make sure the method was called
- testBase.VerifyAll();
- }
Add Comment
Please, Sign In to add comment