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
TypeMockMockedEvent
Namespace: TypeMock
Assembly: TypeMock (in TypeMock.dll) Version: 9.3.6.0 (9.3.6.0)
Syntax
The MockedEvent type exposes the following members.
Properties
Name | Description | |
---|---|---|
![]() | Instance |
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. |
![]() | InstanceCount |
Return the number of instances registered to this mocked event. See Instance |
Methods
Name | Description | |
---|---|---|
![]() | Equals | (Inherited from Object.) |
![]() | Finalize | Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.) |
![]() | Fire |
Fire a mocked event
|
![]() | GetEventHandle |
Retrieve the EventHandle
|
![]() | GetHashCode | Serves as a hash function for a particular type. (Inherited from Object.) |
![]() | GetType | Gets the Type of the current instance. (Inherited from Object.) |
![]() | MemberwiseClone | Creates a shallow copy of the current Object. (Inherited from Object.) |
![]() | ToString | Returns a string that represents the current object. (Inherited from Object.) |
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
// Part of tested class public void Init() { Button button = new Button(); button.Click += new EventHandler(button_Click); }
[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(); }
See Also