chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male

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:

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.