MockStartBlock Method

Typemock Isolator Developer Guide
Start an expectation block

Namespace:  TypeMock
Assembly:  TypeMock (in TypeMock.dll) Version: 9.3.6.0 (9.3.6.0)
Syntax

public ExpectationBlock StartBlock()

Return Value

Type: ExpectationBlock
The Expectation Block
Remarks

Group a number of expectation. When grouping expectations in a block you can control The expectation dynamically. This is very useful when setting up a mock infrastructure. After defining a block it is possible to:

  1. Remove the block using Clear(ExpectationBlock).
  2. Add expectations before the block using StartBlockBefore(ExpectationBlock).
Blocks can be labeled and can be later on referred to by GetBlock(String). Blocks can have different VerifyModes.
Examples

The following example shows a utility method that sets up expectations for an AccessController
private Mock SetupExpectations()
{
    //Mock all invocation of AccessController, 
    Mock mock = MockManager.MockAll(typeof(AccessController));

    // start a block called loginLabel, this will be verified normally
    mock.StartBlock("loginLabel",VerifyMode.Normal);
     // We expect that the login method will be called and we will return 
     // AccessStatus.INVALID_USER the first time and AccessStatus.VALIDATED the 
     // second time
     mock.ExpectAndReturn("Login", AccessStatus.INVALID_USER);
     mock.ExpectAndReturn("Login", AccessStatus.VALIDATED);
    mock.EndBlock();

    // expect another call and return validated.
    mock.ExpectAndReturn("Login", AccessStatus.VALIDATED);
    return mock;
}
[Test]
public void TestSomething()
{
    Mock mock = SetupExpectations();

    // In this test we need to add an expectation before our block
    mock.StartBlockBefore(mock.GetBlock("loginLabel"));
    mock.ExpectAndReturn("Login", AccessStatus.VALIDATED);
    mock.EndBlock();

    // continue with test
}
[Test]
public void TestSomething2()
{
    Mock mock = SetupExpectations();

    // In this test we need to remove the expectation our block
    mock.Clear(mock.GetBlock("loginLabel"));

    // continue with test
}

See Also

Reference