Mocking Interfaces and Abstract Classes

Typemock Isolator can create mocked objects on the fly. Mocking interfaces using Typemock Isolator works the same as mocking concrete types. The difference is that Typemock Isolator will create the new object for you (as opposed to mocking concrete types, where you can create the object yourself. This object (accessed via the additional Object property) can be passed to a tested method. 

Example - Mocking an Interface

Here is an example of how to create a mock for the IList Interface. We test the mocked object within the tests. (This is for demonstration purposes; normal test will pass the object as a parameter.)

Hide C# Code
Hide Visual Basic Code
// C#
[Test]
public void Dynamic()

   // Initialize
   MockManager.Init()
   // Mock our interface
   MockObject mock = MockManager.MockObject(typeof(IList));
   // Lets mock a return of 4 for Count
   mock.ExpectGet("Count",4);
   // Get our concrete mocked Object
   IList list = (IList)mock.Object;
   // Let just test it
   Assert.AreEqual(3,list.Count);
   MockManager.Verify();
}

' Visual Basic
<Test()> Public Sub Dynamic ()
   ' Initialize
   MockManager.Init()
   ' Mock our interface
   Dim mock As MockObject = MockManager.MockObject(GetType(IList)) 
   ' Lets mock a return of 4 for Count
   mock.ExpectGet("Count",4)
   ' Get our concrete mocked Object
   Dim list As IList = mock.Object
   ' Let just test it
  
Assert.AreEqual(3,list.Count)
   MockManager.Verify()
End
Sub

Tip: By default, all mock objects that implement interfaces are Strict.

Tip: When creating a mock object for a concrete class, you can pass the constructor arguments.

Example - Creating a Mocked Instance

Here is an example of how to create a mocked instance of the concrete class- TestedClass, without mocking the constructor and passing "Init" to the constructor.

// C#
[Test]
public void DynamicConcrete()

   // Initialize
   MockManager.Init()
   // Mock our class
   MockObject mock = MockManager.MockObject(typeof(TestedClass),Constructor.NotMocked,"Init");
   // Lets mock a return of 4 for Count
   mock.ExpectGet("Count",4);
   // Get our concrete mocked Object
   TestedClass test = (TestedClass)mock.Object;
   // Let just test it
   Assert.AreEqual(3,test.Count);
   MockManager.Verify();
}

' Visual Basic
<Test()> Public Sub DynamicConcrete ()
   ' Initialize
   MockManager.Init()
   ' Mock our class
   Dim mock As MockObject = MockManager.MockObject(GetType(TestedClass), Constructor.NotMocked, "Init") 
   ' Lets mock a return of 4 for Count
   mock.ExpectGet("Count",4)
   ' Get our concrete mocked Object
   Dim test As TestedClass = mock.Object
   ' Let just test it
  
Assert.AreEqual(3,test.Count)
   MockManager.Verify()
End
Sub


Tip: To mock internal interfaces, do one of the following:

Tip: When the constructor arguments are unknown or not important, Typemock Isolator will automatically create the required arguments to enable the objects creation.To use this feature, simply leave the constructor arguments empty.

Tip: All abstract methods and properties are created with a default implementation. By default, these methods are strict and will fail if they are called without any expectations. To use these you must change the strictness (see Arbitrary Calls).

Here is the default implementation:


Copyright © Typemock Ltd. 2004-2010. All Rights Reserved.