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.)
// 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
By default, all mock objects that implement interfaces are Strict.
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
To mock internal interfaces, do one of the following:
- When an interface is internal, all derived classes must be internal too. Just mock a concrete derived class.
- In .NET 2.0 you can mock the internal interfaces if you give Typemock Isolator the correct permissions, as follows:
In the AssemblyInfo of the internal interface assembly, add
[assembly: InternalsVisibleTo("DynamicMockAssembly")]
The DynamicMockAssembly is the assembly where Typemock Isolator creates all concrete classes derived from interfaces and abstract classes.
- When the assembly is signed, use the following
[assembly: InternalsVisibleTo(MockManager.DynamicMocksAssembly)]
or
[assembly: InternalsVisibleTo("DynamicMockAssembly,PublicKey=0024000004800000940000000602000000
240000525341310004000001000100ab8e3015b99a732d20ecb2a29fb3f54288a8a614896e7c5091d7b9045368fe6b8
bfcc72dce4f01b71281eb4e380dcb709c83a5042a54c684a4711248c078fefb01bcdb09a6ce252e0304ed08c6e4ddf6
9212e3d0a770d953572e3c474fc08fe3bdbb2fad97b32c6045c08f34466dc8e07bd255d3dbc72408dce6859edb4b04bf")]
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.
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:
- All void calls return
- Other methods will return a default value (0 or null depending on the return type)
- Properties will behave like simple properties
Copyright © Typemock Ltd. 2004-2010. All Rights Reserved.