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

  1. 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();
  2. 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();
  3. 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();
  4. 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();

Bookmark at:
Add 'How Hierarchy works with TypeMock' to Del.icio.us Add 'How Hierarchy works with TypeMock' to digg Add 'How Hierarchy works with TypeMock' to reddit Add 'How Hierarchy works with TypeMock' to Feed Me Links! Add 'How Hierarchy works with TypeMock' to Technorati Add 'How Hierarchy works with TypeMock' to Yahoo My Web Add 'How Hierarchy works with TypeMock' to Newsvine Add 'How Hierarchy works with TypeMock' to FURL Add 'How Hierarchy works with TypeMock' to blinklist Add 'How Hierarchy works with TypeMock' to My-Tuts 

31 October 2006 | .NET Tests | Comments | Print This Post

One Response to “How Hierarchy works with TypeMock”

  1. 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 […]

Leave a Reply

  1.  
  2.  
  3.  
Search Eli Lopian’s Blog (TypeMock)

Navigation

Recent Posts

Categories

Archives

Managment