MockedEvent Class

Typemock Isolator Developer Guide
MockedEvent is used to validate firing of events.

To use this an event must be mocked first, and then the test can fire that event to test that the correct methods are actually called.

Inheritance Hierarchy

SystemObject
  TypeMockMockedEvent

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

public class MockedEvent : IMockedEvent

The MockedEvent type exposes the following members.

Properties

  NameDescription
Public propertyInstance
Retrieve the IMockedEvent when more than one instance of the event published is mocked

The first instance to register to the event will be in index 0, the next instance in 1 and so on. This is used to simulate firing the event.

Public propertyInstanceCount
Return the number of instances registered to this mocked event. See Instance
Top
Methods

  NameDescription
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 methodFire
Fire a mocked event
Public methodGetEventHandle
Retrieve the EventHandle
Public methodGetHashCode
Serves as a hash function for a particular type.
(Inherited from Object.)
Public methodGetType
Gets the Type of the current instance.
(Inherited from Object.)
Protected methodMemberwiseClone
Creates a shallow copy of the current Object.
(Inherited from Object.)
Public methodToString
Returns a string that represents the current object.
(Inherited from Object.)
Top
Remarks

When more than one instance of the event published is mocked, use the MockedEvent.Instance[index] property The first instance to register to the event will be in index 0, the next instance in 1 and so on.

Examples

Example of one instance of Button having the Click event mocked. We will fire the event to test that our TestedClass event handler is called
// Part of tested class
public void Init()
{
   Button button = new Button();
   button.Click += new EventHandler(button_Click);
}
Here is the test
[Test]
public void Test()
{
    // we will mock the Click event and save the MockedEvent
    Mock buttonMock = MockManager.Mock(typeof(Button));
    MockedEvent buttonHandle = buttonMock.ExpectAddEvent("Click");

    // we expect button_Click to be called once (when the button is clicked)
    Mock testedMock = MockManager.Mock(typeof(TestedClass));
    testedMock.ExpectCall("button_Click");

    // call our code
    TestedClass test = new TestedClass();
    test.Init();

    // emulate click.
    buttonHandle.Fire(this, EventArgs.Empty);

    // verify that all calls where made
    MockManager.Verify();
}
// Natural Mocks
[Test]
public void Test()
{
    MockedEvent buttonHandle;
       // Start mocking 
    using (RecordExpectations recorder = RecorderManager.StartRecording())
    {
       // we will mock the Click event and save the MockedEvent
       Button buttonMock = new Button();
       buttonMock.Click += null;
       buttonHandle = RecorderManager.LastMockedEvent;

       // we expect button_Click to be called once (when the button is clicked)
       TestedClass testMock = new TestedClass();
       testMock.button_Click(null,null); // use Reflective Mocks if this is private 
    }

    // call our code
    TestedClass test = new TestedClass();
    test.Init();

    // emulate click.
    buttonHandle.Fire(this, EventArgs.Empty);

    // verify that all calls where made
    MockManager.Verify();
}
For VB .NET 1.1 see RecordExpectations

Since Version 3.6
See Also

Reference