MockManager Class

Typemock Isolator Developer Guide
MockManager is the Typemock Isolator manager class, this class is the entry point for creating type mocks
Inheritance Hierarchy

SystemObject
  TypeMockMockManager

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

public sealed class MockManager

The MockManager type exposes the following members.

Properties

  NameDescription
Public propertyStatic memberAny Obsolete.
Obsolete, use Check.IsAny instead, Provided for backward compatibility
Public propertyStatic memberCollectAllCalls
When set Typemock Isolator collects data on ALL method entries, this can then be verified using CalledCounter(String, String)
Public propertyStatic memberCONTINUE_WITH_METHOD
If CONTINUE_WITH_METHOD is returned the mocked method will continue with normal execution. This is an un-mocked expectation that will verify that a method is called only without mocking the method.
Public propertyStatic memberInSpecialLicenseChain
A property that indicates if the licence is part of a special Isolator package.
Public propertyStatic memberIsInitialized
Public propertyStatic memberLocked
Public propertyStatic memberLogFile Obsolete.
Public propertyStatic memberValidateArgsOnVerify Obsolete.
Obsolete. Argument validation is now done both on calling the method and during Verify automatically. Postpone throwing validation errors till Verify is called
Public propertyStatic memberVersion
Return Typemock Isolator version
Top
Methods

  NameDescription
Public methodStatic memberCalledCounter(String, String)
Return the amount of times a method has been called
Public methodStatic memberCalledCounter(Type, String)
Return the amount of times a method has been called
Public methodStatic memberCalledCounter(Type, String, Type)
Return the amount of times a method has been called
Public methodStatic memberCalledCounterTMockType(String)
Return the amount of times a method has been called
Public methodStatic memberCalledCounterTMockType(String, Type)
Return the amount of times a method has been called
Public methodStatic memberCalledIndexerCounter
Return the amount of times an indexer has been called
Public methodStatic memberCalledPropertyGetCounter
Return the amount of times a property has been called
Public methodStatic memberCalledPropertySetCounter
Return the amount of times a property has been set
Public methodStatic memberClearAll
Clears all expectations and mocks
Public methodEquals
Determines whether the specified Object is equal to the current Object.
(Inherited from Object.)
Protected methodFinalize
Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.
(Inherited from Object.)
Public methodGetHashCode
Serves as a hash function for a particular type.
(Inherited from Object.)
Public methodStatic memberGetInstanceMocks(Type)
Returns a list of instance mocks of a type
Public methodStatic memberGetInstanceMocksTMockedType
Returns all instance mocks for TMockedType
Public methodStatic memberGetMockAll(Type)
Returns the mock for all instances of a type, or null if it is not mocked
Public methodStatic memberGetMockAllTMockedType
Returns all instances mock of TMockedType
Public methodStatic memberGetMockedTypes
Returns all types that are mocked
Public methodStatic memberCode exampleGetMockOfT
Returns the mock associated with a specific instance
Public methodStatic memberGetMocks
Returns all mocks
Public methodStatic memberGetMocks(Type)
Returns all mocks for a type
Public methodStatic memberGetMocksTMockedType
Returns all mocks for TMockedType
Public methodGetType
Gets the Type of the current instance.
(Inherited from Object.)
Public methodStatic memberInit
Defaults to collect call information ONLY for mocked classes and not verbose
Public methodStatic memberInit(Boolean)
Manually set Call Collection and verbose behaviors
Public methodStatic memberIsTypeMocked(Type)
Check if a type is mocked (with MockAll or instance mocks)
Public methodStatic memberIsTypeMockedTMockedType
Check if a type is mocked (with MockAll or instance mocks)
Public methodStatic memberIsTypeMockedAll(Type)
Check if a type is mocked for all instances
Public methodStatic memberIsTypeMockedAllTMockedType
Check if a type is mocked for all instances
Protected methodMemberwiseClone
Creates a shallow copy of the current Object.
(Inherited from Object.)
Public methodStatic memberMock(Type)
Automatically mock constructors as well, i.e. constructor code will NOT be called
Public methodStatic memberMock(Type, Boolean) Obsolete.
Create a new Type Mock to intercept and mock calls made on the next instance of a type, with ability to decide if Constructors should be mocked or not (default is to mock constructors)
Public methodStatic memberMock(Type, Constructor)
Create a new Type Mock to intercept and mock calls made on the next instance of a type, with ability to define the scope of mocked Constructors (default is to mock all constructors)
Public methodStatic memberMockTMockedType
Intercept and mock calls made on the next new instance of TMockedType.
Public methodStatic memberMockTMockedType(Constructor)
Intercept and mock calls made on the next new instance of TMockedType, with constructor behavior control
Public methodStatic memberMockAll(Type)
Automatically mock constructors as well, i.e. constructor code will NOT be called
Public methodStatic memberMockAll(Type, Boolean)
Create a new Type Mock for all instances of the type, with ability to decide if Constructors should be mocked or not
Public methodStatic memberMockAll(Type, Constructor)
Create a new Type Mock for all instances of the type, with ability to to define the scope of mocked Constructors (default is to mock all constructors)
Public methodStatic memberMockAllTMockedType
Intercept and mock calls made on the all instance of TMockedType.
Public methodStatic memberMockAllTMockedType(Constructor)
Intercept and mock calls made on the all instance of TMockedType, with constructor behavior control
Public methodStatic memberMockGlobalFunctions
Use this type when mocking global functions
Public methodStatic memberMockObject(Type, Boolean) Obsolete.
Create a new Dynamic Mock Object with ability to decide if Constructors should be mocked or not
Public methodStatic memberCode exampleMockObject(Type, Object)
Automatically mock constructors as well, i.e. constructor code will NOT be called
Public methodStatic memberMockObject(Type, Constructor, Object)
Create a new Dynamic Mock Object with ability to define the scope of mocked Constructors (default is to mock all constructors)
Public methodStatic memberMockObjectTMockedType
Create an instance of TMockedType and mock calls made on the instance.
Public methodStatic memberMockObjectTMockedType(Object)
Create an instance of TMockedType and mock calls made on the instance, while sending constructor arguments
Public methodStatic memberMockObjectTMockedType(Constructor)
Create an instance of TMockedType and mock calls made on the instance, while controlling the constructor
Public methodStatic memberMockObjectTMockedType(Constructor, Object)
Create an instance of TMockedType and mock calls made on the instance, while controlling the constructor and sending constructor arguments
Public methodToString
Returns a string that represents the current object.
(Inherited from Object.)
Public methodStatic memberVerify
Verify all mocked types
Public methodStatic memberVerifyWithTimeout
Wait with timeout (5 seconds) for all expected methods to be called
Public methodStatic memberVerifyWithTimeout(Int32)
Wait with timeout (im milliseconds) for all expected methods to be called
Top
Fields

  NameDescription
Public fieldStatic memberDynamicMocksAssembly
Top
Remarks

Mock object tests replace domain dependencies with mock implementations used only for testing. This strategy does, however, present a technical challenge in certain situations, as the Mock objects can only mock interfaces, and these mocks have to be passed to the function, so no internal instantiation of classes can be mocked

Typemock Isolator to provide "type" mock objects. No concrete mock objects are actually created, but rather Typemock Isolator is used to weave interceptions into all code that is not part of the class under test. In this way, every method call is intercepted. If the method call is one for which mock data has been declared, then that mock data is returned in place of the actual method being run.

For example a LoginView (the class being tested) which as properties of userID and password that are set prior to calling a validate method. Following that call, the status attribute will be set to a string value that is ultimately displayed to the user. The LoginView is responsible for interpreting the status code (USER_VALID, NO_PASSWORD, NO_USERID, INVALID_USER) that is returned from an AccessController, to determine the status message to display.

Examples

public string Validate()
{
    AccessController controller = new AccessController();
    AccessStatus status = controller.Login(m_UserID,m_Password);
    switch (status) 
    {
        case USER_VALID:
            ...
    }
    return returnStatus;
}
{

However, the LoginView class makes use of the login service provided by the AccessController class. The AccessController will probably itself use other components to authenticate the user. For the tests to work it would be necessary to create some test users in the system. The tests that were written to test the behavior of LoginView would in fact also be testing the AccessController, which should of course have its own suite of tests.

The behavior we are trying to test here is to determine that the user gets the correct status message for the different cases of invalid or missing user credentials returned by the AccessController and not the actual validation of those credentials.

Using Typemock Isolator We can intercept the AccessController and return our own value
Examples

[Test]
public void ValidateValidUser()
{
    //Mock all invocation of AccessController, all calls to this class, are 
    //intercepted.
    Mock mock = MockManager.Mock(typeof(AccessController));

    //We expect that the login method will be called (once) and we will return 
    //AccessStatus.INVALID_USER.
    mock.ExpectAndReturn("Login", AccessStatus.INVALID_USER);

    LoginView view = new LoginView();
    view.setUserID("jane");
    view.setPassword("passAugust");
    // run the method being tested
    view.Validate();
    Assert.AreEqual("login successful", view.Status);

    // Verify that all the expected calls have actually been called.
    MockManager.Verify();
}

Note Note
To enable mock manager do one of the following
  1. Run mocking_on.bat
  2. Run Typemock Isolator [application]
  3. Enable Visual Studio Plugin

See Also

Reference