Understanding Instance Mocking

Every time a type is mocked, TypeMock will mock the NEXT invocation of that type. Thus, classes that are instantiated somewhere in the code can still be mocked. This is the reason why it is possible to mock more than one instance of a type and expect different behavior from each instance. There is also a possibility to mock all instances of a type using MockManager.MockAll, which will mock all objects of the mocked type.

The following list explains the different ways to mock:

Tip: If you do not want to mock some instances, just mock the type without setting any expectations.

Example - Mocking Instances

In this example, there is a TestedClass class that internally creates three instances of YourClass. We want to mock only the third instance of YourClass and set up expectations for that instance.

This is the TestedClass code.

Hide C# Code
Hide Visual Basic Code
// C#
public class
TestedClass
{
   // This is bas design but is used just to show the abilities of Typemock Isolator
   public int something;
   // public method
   public void PublicMethod()
   {
      PrivateMethod();
   }
   // private method will instantiate our class
   private void PrivateMethod()
   {
      YourClass c1 = new YourClass();
      YourClass c2 = new YourClass();
      YourClass c3 = new YourClass();
      something = c3.DoSomething();
   }
}

' Visual Basic
Public Class
TestedClass
   ' This is bas design but is used just to show the abilities of Typemock Isolator
   Public something As Integer
   ' public method
   Public Sub PublicMethod()
      PrivateMethod()
   End Sub
   ' private method will instantiate our class
   Private Sub PrivateMethod()
      Dim c1 As YourClass = New YourClass
      Dim c2 As YourClass = New YourClass
      Dim c3 As YourClass = New YourClass
      something = c3.DoSomething()
   End Sub
End Class

And this is the test.

[Test]
public
void ThirdInstance ()
{
      // Initialize Type Mocks (This can be done in the [Setup])
      MockManager.Init ();
      // the YourClass class is now being mocked. Twice for first 2 objects
      // We wont add any expectations to these mocks - they will run as normal
    1 MockManager.Mock(typeof(YourClass)); 
      MockManager.Mock(typeof(YourClass)); 
      // Mock and add expectations for 3rd instance
      Mock mock = MockManager.Mock(typeof(YourClass)); 
      // set up our expectations 
    2 mock.ExpectAndReturn("DoSomething",5);

      // lets run our test
      TestedClass t = new TestedClass();
      t.PublicMethod();     

      // something should be set to 5
      Assert.IsEqual(5,t.something);        
      // Verify that all calls were made )This can be done in the [Teardown]
      MockManager.Verify();
}

' Visual Basic
<Test()> Public Sub ThirdInstance ()
      ' Initialize Type Mocks (This can be done in the <Setup>)
      MockManager.Init()
      ' the YourClass class is now being mocked. Twice for first 2 objects
      ' We wont add any expectations to these mocks - they will run as normal
    1 MockManager.Mock(GetType(YourClass)) 
      MockManager.Mock(GetType(YourClass)) 
      ' Mock and add expectations for 3rd instance
      Dim mock As Mock = MockManager.Mock(GetType(YourClass)) 
      ' set up our expectations 
    2 mock.ExpectAndReturn("DoSomething",5)

      // lets run our test
      Dim t As TestedClass = new TestedClass
      t.PublicMethod();    
     
      ' something should be set to 5
      Assert.IsEqual(5,t.something);  
      ' Verify that all calls were made (This can be done in the <Teardown>)
      MockManager.Verify()
End Sub

This is what we did:

  1 Mocked the first two instances of YourClass - no expectations are set so the next two new YourClass instances run as normal.
  2 Mocked the next (third) instance of YourClass - this time DoSomething was mocked and returned 5.
Example - Mocking All Instances

We will now use the same TestedClass class but mock all instances of the YourClass.

[Test]
public
void AllInstances ()
{
      // Initialize Type Mocks (This can be done in the [Setup])
      MockManager.Init ();
      // All the YourClass classes will now be mocked.
    1 Mock mock = MockManager.MockAll(typeof(YourClass)); 
      // set up our expectations 
    2 mock.AlwaysReturn("DoSomething",5);

      // lets run our test
      TestedClass t = new TestedClass();
      t.PublicMethod();     

      // something should be set to 5
      Assert.IsEqual(5,t.something);        
      // Verify that all calls were made )This can be done in the [Teardown]
      MockManager.Verify();
}

' Visual Basic
<Test()> Public Sub AllInstances ()
      ' Initialize Type Mocks (This can be done in the <Setup>)
      MockManager.Init()
      ' the YourClass class is now being mocked. Twice for first 2 objects
      ' We wont add any expectations to these mocks - they will run as normal
    1 Dim mock As Mock = MockManager.MockAll(GetType(YourClass)) 
      ' set up our expectations 
    2 mock.AlwaysReturn("DoSomething",5)

      // lets run our test
      Dim t As TestedClass = new TestedClass
      t.PublicMethod();    
     
      ' something should be set to 5
      Assert.IsEqual(5,t.something);  
      ' Verify that all calls were made (This can be done in the <Teardown>)
      MockManager.Verify()
End Sub

This is what we did:

  1 Mocked all instances of YourClass.
  2 Always mocked DoSomething and returned 5.

Tip: When you use MockAll and repeat-sometimes expectations (ExpectAndReturn, for example), only the first instance that will call the method is mocked (once).

Tip: To create a mocked object, see Dynamic Mock Objects.


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