Use Blocks with your mocks

It seems to me that not to many users of Typemock Isolator knows about the feature called expectation block.
This feature can be handy when you find that you are duplicating the same mocks setup across few tests with maybe some minor changes.

Before I start just note that this is an enterprise feature.

Here is an example:

  class ClassToMock
 {
     public void A()
     {}

     public int B()
     {
         return 1;
     }

     public string C()
     {
         return string.Empty;
     }
 }

 [TestFixture]
 public class TestClass
 {     
     private  Mock SetUpExpectation()
     {
         Mock mock = MockManager.Mock();

         mock.StartBlock("ClassToMockBlock", VerifyMode.Normal);
         mock.ExpectCall("A");
         mock.ExpectAndReturn("B", 5);
         mock.EndBlock();

         mock.ExpectAndReturn("C", "Isolator");
         return mock;
     }

     [Test]
     public void TestMethod1()
     {
         // Get the block with expectation for A() B() and C()
         Mock mock = SetUpExpectation();

         ClassToMock c = new ClassToMock();
         c.A();
         Assert.AreEqual(5, c.B());
         Assert.AreEqual("Isolator", c.C());

         MockManager.Verify();
     }

     [Test]
     public void TestMethod2()
     {        
         Mock mock = SetUpExpectation();
         // Everything inside the block is cleared
         // This leaves us with one expectation for method C()
         // We set the expectation for C() after the block ends.
         mock.Clear(mock.GetBlock("ClassToMockBlock"));

         ClassToMock c = new ClassToMock();
         Assert.AreEqual("Isolator", c.C());
         MockManager.Verify();
     }

     [Test]
     public void TestMethod3()
     {
         Mock mock = SetUpExpectation();
         mock.StartBlockBefore(mock.GetBlock("ClassToMockBlock"));
         // Add one more expectation for method A()
         // So we have expectation for A() A() B() and C()
         mock.ExpectCall("A");
         mock.EndBlock();

         ClassToMock c = new ClassToMock();
         c.A();
         Assert.AreEqual(5, c.B());
         Assert.AreEqual("Isolator", c.C());
         c.A();

         MockManager.Verify();
     }
 }

Unlike most real test ClassToMock is used directly in the test methods for sake of clarity. It has three methods A() B() and C()

The interesting code starts at line 20 at SetUpExpectation method.
Here we give the block a name and set the block expectations.
Note that there is one call to C() outside the block, This means that any action
We will do on the block will not affect this expectation.

Now lets see how we can use the block in our tests.
In TestMethod1 I simply use the block ‘as is’ the mock that returned from SetUpExpectation method in line 20 has expectations for A() and B() inside the block and for method C() outside the block. That means that the actions we will do on the block will not affect method C()
You can see this in TestMethod2 we clear all expectation on the block but since C() is outside the block so we left only with expectation on C().
In TestMethod3 we use mock.StartBlockBefore() method to add expectation for method A() to our block. That means we’ll have two expectation for method A().

That’s the basic actions for mock Blocks. Please note that you can use more than one block for the same mock so you can do do ‘more advanced’ tricks with it.

TOP