Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore "null handlers" when a substituted event is raised #667

Merged
merged 3 commits into from
Oct 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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