chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
Welcome to Typemock Community! Here you can ask and receive answers from other community members. If you liked or disliked an answer or thread: react with an up- or downvote.
0 votes
Is there any way to verify that the RaiseEvent statement was called in my Class Under Test?
I don't want to fire the event.

Thanks and regards,
Jimmy
asked by seow (1.2k points)

5 Answers

0 votes
Jimmy hi,

given that your "Class Under Test" look somethign like:
    
public delegate void SampleEventDelegate(object sender, EventArgs e);
   
public class ClassUnderTest
{
     public event SampleEventDelegate SampleEvent;

     // A method that triggers the event:
     public void FireEvent()
     {
        SampleEvent(this,null);
     }
}


You can mock the "FireEvent" call like any call to other method:
[Test]
public void TestEvent()
{
  Mock mock = MockManager.Mock(typeof(ClassUnderTest));
  mock.ExpectCall("FireEvent");
            
  // Create an instance of the ClassUnderTest class.
  ClassUnderTest tested = new ClassUnderTest();
  // Specify the method that will be triggered by the SampleEvent event.
  tested.SampleEvent += delegate { System.Console.WriteLine("Hello, World!"); };
  // Trigger the event
  tested.FireEvent();

  MockManager.Verify();
}


I hope this is what you need. If not please let me know
answered by lior (13.2k points)
0 votes
The idea is to mock the actual handler method and verify that it has been called.
suppose you have:
Private Sub mText_UpdateTime(ByVal Countdown As Double) _
                             Handles mText.UpdateTime
    TextBox1.Text = Format(Countdown, "##0.0")
End Sub

where
mText = New TimerState

and
Class TimerState
    Public Event UpdateTime(ByVal Countdown As Double)
    Public Sub CallRaiseEvent()
        RaiseEvent UpdateTime(Duration - SoFar)
    End Sub
End Class

to check that RaiseEvent works we can mock mText_UpdateTime and Verify that it has been called.

<Test()> Public Sub TestRasieEvent()
      ' the handler class is now being mocked
      Dim handleMock As Mock = MockManager.Mock(Of HandlerEvents) 
      ' mock 1 call to mText_UpdateTime
      handleMock.ExpectCall("mText_UpdateTime")
      
      ' run test 
      Dim classUnderTest = New TimerState
      classUnderTest.CallRaiseEvent()
                   
       ' Verify that mText_UpdateTime was called (and mocked)
      MockManager.Verify()
End Sub
answered by scott (32k points)
0 votes
"Of HandlerEvents" ???
I couldn't find any classes with this name.
answered by seow (1.2k points)
0 votes
Its the name of the class that mText_UpdateTime is defined in.
you can write
MockManager.Mock(GetType(HandlerEvents)) 
answered by scott (32k points)
0 votes
Thanks a lot. Got it working.
answered by seow (1.2k points)
...