# Raise

Use `Mock.Raise` to fire a mock event directly from your test code, at the exact point in the test you choose. You can raise custom or standard events.

`Mock.Raise` differs from [`Raises`](https://www.telerik.com/products/justmock/documentation/basic-usage/mock/raises): `Raises` is chained on `Mock.Arrange` and fires the event automatically when a specific method is called. `Mock.Raise` fires the event immediately when you call it in the test.

## Raising Custom Events

Assume we have the following interface:

```C#
public delegate void CustomEvent(string value);

public interface IFoo
{
    event CustomEvent CustomEvent;
}
```

```VB
Public Delegate Sub CustomEvent(value As String)

Public Interface IFoo
    Event CustomEvent As CustomEvent
End Interface
```

Next is an example on how to use `Raise` to fire custom event.

```C#
[TestMethod]
public void ShouldInvokeMethodForACustomEventWhenRaised()
{
    string expected = "ping";
    string actual = string.Empty;

    // Arrange
    var foo = Mock.Create<IFoo>();

    foo.CustomEvent += delegate(string s)
    {
        actual = s;
    };

    // Act
    Mock.Raise(() => foo.CustomEvent += null, expected);

    // Assert
    Assert.AreEqual(expected, actual);
}
```

```VB
<TestMethod>
Public Sub ShouldInvokeMethodForACustomEventWhenRaised()
    Dim expected As String = "ping"
    Dim actual As String = String.Empty

    ' Arrange
    Dim foo = Mock.Create(Of IFoo)()

    AddHandler foo.CustomEvent, Sub(s As String) actual = s

    ' Act
    Mock.Raise(Sub() AddHandler foo.CustomEvent, Nothing, expected)

    ' Assert
    Assert.AreEqual(expected, actual)
End Sub
```

We use `Raise` to raise `foo.CustomEvent` and pass "ping" to it. Before acting we have attached a delegate to the event. Executing the delegate will result in assigning the passed string to `actual`. Finally, we verify that `expected` and `actual` have the same value.

## Raising Standard Events

Assume we have the following system under test:

```C#
public interface IExecutor<T>
{
    event EventHandler<FooArgs> Done;
}

public class FooArgs : EventArgs
{
    public FooArgs()
    {
    }

    public FooArgs(string value)
    {
        this.Value = value;
    }

    public string Value { get; set; }
}
```

```VB
Public Interface IExecutor(Of T)
    Event Done As EventHandler(Of FooArgs)
End Interface

Public Class FooArgs
    Inherits EventArgs
    Public Sub New()
    End Sub

    Public Sub New(value As String)
        Me.Value = value
    End Sub

    Public Property Value() As String
        Get
            Return m_Value
        End Get
        Set(value As String)
            m_Value = value
        End Set
    End Property
    Private m_Value As String
End Class
```

An example on how to use `Raise` to fire standard event would look like this:

```C#
[TestMethod]
public void ShouldRaiseEventWithStandardEventArgs()
{
    string actual = null;
    string expected = "ping";

    // Arrange
    var executor = Mock.Create<IExecutor<int>>();

    executor.Done += delegate(object sender, FooArgs args)
    {
        actual = args.Value;
    };

    // Act
    Mock.Raise(() => executor.Done += null, new FooArgs(expected));

    // Assert
    Assert.AreEqual(expected, actual);
}
```

```VB
<TestMethod>
Public Sub ShouldRaiseEventWithStandardEventArgs()
    Dim actual As String = Nothing
    Dim expected As String = "ping"

    ' Arrange
    Dim executor = Mock.Create(Of IExecutor(Of Integer))()

    AddHandler executor.Done, Sub(sender As Object, args As FooArgs) actual = args.Value

    ' Act
    Mock.Raise(Sub() AddHandler executor.Done, Nothing, New FooArgs(expected))

    ' Assert
    Assert.AreEqual(expected, actual)
End Sub
```

Here we use `Raise` to raise a standard event - `executor.Done` accepting `FooArgs` object. The attached delegate sets the `Value` property in `FooArgs` object to the variable `actual`. Finally, we verify that `expected` and `actual` have the same value.

## See Also

* [Call Original](https://www.telerik.com/products/justmock/documentation/basic-usage/mock/call-original)

* [Do Nothing](https://www.telerik.com/products/justmock/documentation/basic-usage/mock/do-nothing)

* [Do Instead](https://www.telerik.com/products/justmock/documentation/basic-usage/mock/do-instead)

* [Must Be Called](https://www.telerik.com/products/justmock/documentation/basic-usage/mock/must-be-called)

* [Raises](https://www.telerik.com/products/justmock/documentation/basic-usage/mock/raises)

* [Returns](https://www.telerik.com/products/justmock/documentation/basic-usage/mock/returns)

* [Throws](https://www.telerik.com/products/justmock/documentation/basic-usage/mock/throws)
