Firing Events

To fire static or instance events, use Isolate.Invoke.Event().

When to Use

When your test requires an event to be fired.

Syntax

C#

Isolate.Invoke.Event(() => <event> += <optional list of arguments>);

When no arguments are sent, a default of (this, EventArgs.Empty) is sent.

VB

Isolate.Invoke.Event(Sub() AddHandler <Event>, Function() <Method_Name>,<optional list of arguments>)

Samples

The following sample shows how to fire an event.

C#

[TestMethod, Isolated]
public void FireEvent_RunEvent()
{
  var underTest = new ClassUnderTest();
  var counter = new Counter(underTest);
  // Note how adding a dummy event is the way to fire it
  Isolate.Invoke.Event(() => underTest.RunEvent += null, 0);
  Assert.AreEqual(1, counter.Times);
}

public class Counter
{
  public int Times { get; set; }

  public Counter(ClassUnderTest underTest)
  {
    underTest.RunEvent += new Action(underTest_RunEvent);
  }

  void underTest_RunEvent(int obj)
  {
    Times++;
  }
}

public class ClassUnderTest
{
  public event Action<int> RunEvent;

  public ClassUnderTest() { }
}

VB

<TestMethod, Isolated>
Public Sub FireEvent_RunEvent()
    Dim underTest = New ClassUnderTest()
    Dim counter = New Counter(underTest)

    ' Note how adding a dummy event is the way to fire it
    Isolate.Invoke.Event(Sub() AddHandler underTest.RunEvent, Function() Nothing)
    Assert.AreEqual(1, counter.Times)
End Sub

Public Class Counter
    Public Property Times() As Integer
        Get
            Return m_Times
        End Get
        Set
            m_Times = Value
        End Set
    End Property
    Private m_Times As Integer

    Public Sub New(underTest As ClassUnderTest)
        AddHandler underTest.RunEvent, AddressOf underTest_RunEvent
    End Sub

    Private Sub underTest_RunEvent(obj As Integer)
        Times += 1
    End Sub

End Class

Public Class ClassUnderTest
    Public Event RunEvent As Action(Of Integer)

    Public Sub New()
    End Sub
End Class