







![]() |
Moq |
IMock..::.CreateEventHandler<(Of <(TEventArgs>)>) Method |
IMock Interface Example See Also Send Feedback |
Creates a handler that can be associated to an event receiving
the given TEventArgs 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<TEventArgs> CreateEventHandler<TEventArgs>() where TEventArgs : EventArgs |
Type Parameters
- TEventArgs
- Type of EventArgs data passed in to the event.
Examples
This example shows how to invoke an event with a custom event arguments
class 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<OrderEventArgs>(); var presenter = new OrdersPresenter(mockView.Object); // Check that the presenter has no selection by default Assert.Null(presenter.SelectedOrder); // Create a mock event handler of the appropriate type var handler = mockView.CreateEventHandler<OrderEventArgs>(); // Associate it with the event we want to raise mockView.Object.Cancel += handler; // Finally raise the event with a specific arguments data handler.Raise(new OrderEventArgs { Order = new Order("moq", 500) }); // Now the presenter reacted to the event, and we have a selected order Assert.NotNull(presenter.SelectedOrder); Assert.Equal("moq", presenter.SelectedOrder.ProductName);