Raises the event referenced in eventExpression using the given sender and args arguments.

Namespace:  Moq
Assembly:  Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
public void Raise(
	Action<T> eventExpression,
	EventArgs args
)

Parameters

eventExpression
Type: Action<(Of <(T>)>)

[Missing <param name="eventExpression"/> documentation for "M:Moq.Mock`1.Raise(System.Action{`0},System.EventArgs)"]

args
Type: EventArgs

[Missing <param name="args"/> documentation for "M:Moq.Mock`1.Raise(System.Action{`0},System.EventArgs)"]

Examples

The following example shows how to raise a PropertyChanged()()() event:
CopyC#
var mock = new Mock<IViewModel>();

mock.Raise(x => x.PropertyChanged -= null, new PropertyChangedEventArgs("Name"));

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 presenter = new OrdersPresenter(mockView.Object);

// Check that the presenter has no selection by default
Assert.Null(presenter.SelectedOrder);

// Raise the event with a specific arguments data
mockView.Raise(v => v.SelectionChanged += null, 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);

Exceptions

ExceptionCondition
ArgumentException The args argument is invalid for the target event invocation, or the eventExpression is not an event attach or detach expression.

See Also