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

Fix event subscription/unsubscription with null handler #2034

Merged
merged 3 commits into from
Jun 19, 2024

Conversation

thomaslevesque
Copy link
Member

Fixes #2033

Comment on lines +530 to +554
[Scenario]
public void SubscribeWithNullHandler(IEvents fake, Exception? exception)
{
"Given a fake with an event"
.x(() => fake = A.Fake<IEvents>());

"When subscribing to the event with a null handler"
.x(() => exception = Record.Exception(() => fake.UnsubscribedEvent += null));

"Then it should not throw"
.x(() => exception.Should().BeNull());
}

[Scenario]
public void UnsubscribeWithNullHandler(IEvents fake, Exception? exception)
{
"Given a fake with an event"
.x(() => fake = A.Fake<IEvents>());

"When unsubscribing from the event with a null handler"
.x(() => exception = Record.Exception(() => fake.UnsubscribedEvent -= null));

"Then it should not throw"
.x(() => exception.Should().BeNull());
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't love adding these specs to EventRaisingSpecs, which is about raising events, not subscribing or unsubscribing. But we don't have specs for event subscription/unsubscription, and I also don't love adding a new spec file just for that.

Arguably, EventRaisingSpecs covers not only event raising, but also subscription/unsubscription. So maybe we should just rename it to EventSpecs or something like that.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree. Renaming to EventSpecs suits me. We can do here or later, as pleases you.

@blairconrad
Copy link
Member

Thanks, @thomaslevesque. Your changes look sufficient to me. But they were bigger than I anticipated. I may be missing something, but blairconrad@c0091b1 also makes the specs pass with less disruption to the original code (and less mucking about with nullable hints). Thoughts?

@thomaslevesque
Copy link
Member Author

I like your approach! It's simpler, and doesn't require EventCall.EventHandler to be nullable.
Do you mind if I just cherry-pick your commit to replace my changes?

@blairconrad
Copy link
Member

Do you mind if I just cherry-pick your commit to replace my changes?

I do. I'm very protective of my IP.

Don't be silly.

Copy link
Member

@blairconrad blairconrad left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, @thomaslevesque!

@blairconrad blairconrad merged commit 94caef0 into FakeItEasy:master Jun 19, 2024
5 checks passed
@thomaslevesque thomaslevesque deleted the fix-null-event-handler branch June 19, 2024 14:01
@thomaslevesque
Copy link
Member Author

Thanks for the assist!

@blairconrad
Copy link
Member

teamwork

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Adding null-valued EventHandlers to a fake leads to ArgumentNullException
2 participants