Overview
There are two main methods for mocking classes:
- Mocking each type separately and defining the behavior using string-based (reflective) expectations
- Using Natural Mocks™ that supports refactoring and can mock a chain of calls
Reflective type mocks require two main entry classes:
- MockManager - this
is the entry point to the framework; all methods are static, allowing you to use the framework with customized test classes
- Mock - this is the control for the mocked type, where expectation are set
MockManager Class
The MockManager class has three major methods
- Init() - initialized type mocking; must be called before every test that uses Type Mocks
- Mock(Type) - mocks Type and returns the Mock object used to control the type; after this is called, the next new Type instance will be mocked, and all static calls will be intercepted (see Understanding Instance Mocking and Dynamic Mock Objects)
- Verify() - completes the tests by verifying all mock type; it must be called after every test that used Type Mocks (see Attributes for a cleaner way to verify the mocks)
Mock Class
The Mock class has the following major methods:
- Call Expectations
The following methods are used to intercept void calls that do not return any values:
ExpectCall - expect one (or more) calls and mock it/them (see The First Mock)
ExpectAlways - Expect unlimited calls
ExpectUnmockedCall - Expect one (or more) call/s, without mocking, with the ability to verify the arguments (see Unmocked Expectations)
- Returning Values
The following methods are used to intercept calls that return a value:
ExpectAndReturn - expect one (or more) call/s and mock a return value (see Returning Values)
AlwaysReturn - expect unlimited calls and always return the same mock value
- Throwing Exceptions
The following methods are used to intercept calls and mock throwing an exception:
ExpectAndThrow - expect one (or more) call/s and mock an exception (see Throwing exceptions)
AlwaysThrow - expect unlimited calls and always throw the same exception
- Parameter Checking
It is possible to check parameters by calling Arg() with the expected argument list (see Checking Arguments)
Copyright © Typemock Ltd. 2004-2009. All Rights Reserved.