Skip to content

Commit

Permalink
Merge pull request #667 from x789/issue/647
Browse files Browse the repository at this point in the history
Ignore "null handlers" when a substituted event is raised
  • Loading branch information
dtchepak authored Oct 10, 2021
2 parents 680df07 + 4e6be02 commit b119cc3
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/NSubstitute/Routing/Handlers/RaiseEventHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ public RouteAction Handle(ICall call)
var handlers = _eventHandlerRegistry.GetHandlers(eventInfo.Name);
foreach (Delegate handler in handlers)
{
if (handler == null)
{
continue;
}

try
{
handler.DynamicInvoke(eventArguments);
Expand Down
29 changes: 29 additions & 0 deletions tests/NSubstitute.Acceptance.Specs/EventChecking.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,35 @@ public void Check_if_event_was_subscribed_to()
Assert.Throws<ReceivedCallsException>(() => engine.Received().Started += someOtherHandler);
}

[Test]
public void Check_if_nullHandlers_are_ignored()
{
var raised = false;
var source = Substitute.For<IEngine>();
source.Started += null;
source.Started += () => raised = true;
source.Started += Raise.Event<Action>();

Assert.IsTrue(raised);
}

[Test]
public void Check_if_multiple_handlers_get_called()
{
var raised1 = false;
var raised2 = false;
var raised3 = false;
var source = Substitute.For<IEngine>();
source.Started += () => raised1 = true;
source.Started += () => raised2 = true;
source.Started += () => raised3 = true;
source.Started += Raise.Event<Action>();

Assert.IsTrue(raised1, "The first handler was not called");
Assert.IsTrue(raised2, "The second handler was not called");
Assert.IsTrue(raised3, "The third handler was not called");
}

public interface IEngine
{
event Action Started;
Expand Down

0 comments on commit b119cc3

Please sign in to comment.