I have an event and a raising method as below :
public class Events {
public event EventHandler<CustomEventArgs> Succeed;
public virtual void OnSucceed(object sender, params object[] data)
{
CustomEventArgs args = new CustomEventArgs(data);
EventHandler<CustomEventArgs> _succeed = Succeed;
if (_succeed != null)
{
_succeed(sender, args);
}
}}
I have created a unit test for the OnSucceed method (using FluentAssertions ):
[Test]
public void SucceedShouldNotBeRaisedTest()
{
Events events = new Events();
events.MonitorEvents();
events.OnSucceed(this,"somedata");
events.ShouldNotRaise("Succeed");
}
as there is no subscriber to the event then I expect it not to raise Succeed event
but the test fails as Succeed event is raised . what’s wrong with this ?!
When you call
events.MonitorEvents();, FluentAssertions automatically subscribe to the public events to detect when an event has been raised.Your test is failing because your condition will always evaluate to
true:if (_succeed != null). When testing, the event will always be different fromnullNow I would like to recommend you to following approach proposed by Jon Skeet:
With the above event declaration, your event will never be
null(an event cannot be assigned from outside of its class)Note: You can assign the delegate behind the event to null inside its class like this:
The above statement is assigning the delegate behind the event to null, not the event itself. Usually you won’t need to do something like this, but in case you do, you would have to re-initialize the event like this:
If you follow these suggestions, your event will never be null and you won’t need to call the
if(this.MyEvent != null)condition in order to raise your events anymore. (Note that this condition was totally technical and it’s not related to the domain itself.)Now that you have removed that technical condition you can actually focus in the domain rules to decide when to raise the event.
The last step would be to remove:
if (_succeed != null)and add a condition indicating if the event should or should not be raised based on your current domainFor your tests, you just need to configure your Subject Under Test with the required conditions in order to raise or not raise your events.
Full sample: