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
I have a class (C#) that throws an event. I wish to catch the fact that the event is thrown via mocking. What's the simplest way to do it? Note that unlike the example in your documentation I am not testing the event hook up mechanism, as that happens outside of the scope of the class that I'm testing.

Thanks!
________
HEALTH STORE
asked by scotb (1.2k points)

2 Answers

0 votes
I guess that the simplist way is to mock the method that will be called when the event is fired.
e.g.
public void event_Fired(object sender, EventArgs e)
{
}
...
// test that the event is actually thrown
using (RecordExpectations recorder = RecorderManager.StartRecording())
{
   // expect event_Fired to be called once
   event_Fired(null,null);
   // uncomment next line to call original method
   // recorder.CallOriginal();
}

// lets register our event handler and see if it is called
TestedClass tested = new TestedClass ();
tested.MyEvent += event_Fired;
tested.FireTheEvent(...);

// fail if event_Fired was not called
MockManager.Verify();
answered by scott (32k points)
0 votes
That's what I figured. Thanks for the confirmation.
________
Vapormatic
answered by scotb (1.2k points)
...