New to Telerik JustMock? Download free 30-day trial

Raise

The Raise method is used for raising mocked events. You can use custom or standard events.

Raising Custom Events

Assume we have the following interface:

public delegate void CustomEvent(string value); 
 
public interface IFoo 
{ 
    event CustomEvent CustomEvent; 
} 
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.

[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); 
} 
<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:

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; } 
} 
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:

[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); 
} 
<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

In this article