CreateEventHandler Method

Moq 2.6

Creates a handler that can be associated to an event receiving a generic EventArgs and can be used to raise the event.

Namespace:  Moq
Assembly:  Moq (in Moq.dll) Version: 2.6.1014.1 (2.6.0.0)

Syntax

C#
MockedEvent<EventArgs> CreateEventHandler()

Examples

This example shows how to invoke a generic event in a view that will cause its corresponding presenter to react by changing its state:
CopyC#
var mockView = new Mock<IOrdersView>();
var mockedEvent = mockView.CreateEventHandler();

var presenter = new OrdersPresenter(mockView.Object);

// Check that the presenter is not in the "Canceled" state
Assert.False(presenter.IsCanceled);

// Create a mock event handler of the appropriate type
var handler = mockView.CreateEventHandler();
// Associate it with the event we want to raise
mockView.Object.Cancel += handler;
// Finally raise the event
handler.Raise(EventArgs.Empty);

// Now the presenter reacted to the event, and changed its state
Assert.True(presenter.IsCanceled);

See Also