How Hierarchy works with TypeMock
Mocking methods in the hierarchy of the code is supporting in TypeMock.NET.
There is nothing like examples so here is our hierarchy.
We are mocking type Second
-
A mocked method, will not be mock when called from a derived class.
Example: Mocking DoSomething() will not be mocked if it is called from Third.
Mock mock = MockManager.Mock(typeof (Second)); mock.ExpectCall("DoSomething"); Third third = new Third(); // this won’t be mocked third.DoSomething(); -
A mocked method defined in a base type will be mocked only when called from the mocked type.
Example: Mocking InFirst() will only be mocked when called from Second.Mock mock = MockManager.Mock(typeof (Second)); mock.ExpectCall("InFirst"); Second second = new Second(); // Will be mocked even if it is defined in First second.InFirst(); -
An overloaded method will be mocked in the base class too when called from the mocked type.
Example: Mocking Overridden() will be mocked also when Second calls base.Overridden()
// if we call the base public void DoSomething() { base.Overridden(); } Mock mock = MockManager.Mock(typeof (Second)); mock.ExpectCall("Overridden"); Second second = new Second(); // base.Overridden will be mocked second.DoSomething(); -
To specifically mock only a base method use the new: mock.CallBase
Example: Mocking Overriden() will be mocked ONLY when Second calls base.Overridden()
// if this is the method public override void Overridden() { base.Overridden(); } Mock mock = MockManager.Mock(typeof (Second)); mock.CallBase.ExpectCall("Overridden"); Second second = new Second(); // here the Second.Overridden will be called and First.Overridden Mocked second.Overridden();
2 Responses to “How Hierarchy works with TypeMock”
1 Eli Lopian’s Blog (TypeMock) » Blog Archive » New and Noteworthy - Version 3.6.1 3 November 2006 @ 2:52 pm
[…] It is possible to mock ‘hidden’ methods.Mock overridden base methods with CallBaseMock overloaded Static methods with CallStatic […]
2 Antonio 20 December 2009 @ 10:08 am
I esteem the important knowledge you recommend in your post. I will bookmark your blog and have my kid check up here regularly. I am rather sure they will take course a huge of new stuff here than anybody else!
Leave a Reply