Raise Method (Action(T), EventArgs)

Moq

Collapse image Expand Image Copy image CopyHover image
Raises the event referenced in eventExpression using the given args argument.

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

Syntax

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

Parameters

eventExpression
Type: System..::..Action<(Of <(<'T>)>)>
args
Type: 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

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

See Also