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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
<span class="kwrd">class</span> ClassToMock { <span class="kwrd">public</span> <span class="kwrd">void</span> A() {} <span class="kwrd">public</span> <span class="kwrd">int</span> B() { <span class="kwrd">return</span> 1; } <span class="kwrd">public</span> <span class="kwrd">string</span> C() { <span class="kwrd">return</span> <span class="kwrd">string</span>.Empty; } } [TestFixture] <span class="kwrd">public</span> <span class="kwrd">class</span> TestClass { <span class="kwrd">private</span> Mock SetUpExpectation() { Mock mock = MockManager.Mock(); mock.StartBlock(<span class="str">"ClassToMockBlock"</span>, VerifyMode.Normal); mock.ExpectCall(<span class="str">"A"</span>); mock.ExpectAndReturn(<span class="str">"B"</span>, 5); mock.EndBlock(); mock.ExpectAndReturn(<span class="str">"C"</span>, <span class="str">"Isolator"</span>); <span class="kwrd">return</span> mock; } [Test] <span class="kwrd">public</span> <span class="kwrd">void</span> TestMethod1() { <span class="rem">// Get the block with expectation for A() B() and C()</span> Mock mock = SetUpExpectation(); ClassToMock c = <span class="kwrd">new</span> ClassToMock(); c.A(); Assert.AreEqual(5, c.B()); Assert.AreEqual(<span class="str">"Isolator"</span>, c.C()); MockManager.Verify(); } [Test] <span class="kwrd">public</span> <span class="kwrd">void</span> TestMethod2() { Mock mock = SetUpExpectation(); <span class="rem">// Everything inside the block is cleared</span> <span class="rem">// This leaves us with one expectation for method C()</span> <span class="rem">// We set the expectation for C() after the block ends.</span> mock.Clear(mock.GetBlock(<span class="str">"ClassToMockBlock"</span>)); ClassToMock c = <span class="kwrd">new</span> ClassToMock(); Assert.AreEqual(<span class="str">"Isolator"</span>, c.C()); MockManager.Verify(); } [Test] <span class="kwrd">public</span> <span class="kwrd">void</span> TestMethod3() { Mock mock = SetUpExpectation(); mock.StartBlockBefore(mock.GetBlock(<span class="str">"ClassToMockBlock"</span>)); <span class="rem">// Add one more expectation for method A()</span> <span class="rem">// So we have expectation for A() A() B() and C()</span> mock.ExpectCall(<span class="str">"A"</span>); mock.EndBlock(); ClassToMock c = <span class="kwrd">new</span> ClassToMock(); c.A(); Assert.AreEqual(5, c.B()); Assert.AreEqual(<span class="str">"Isolator"</span>, 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.