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

Nullable suppression for Assert.Throws does not work in presence of a Assert.Multiple block #342

Closed
smkanadl opened this issue Jan 19, 2021 · 4 comments · Fixed by #343
Closed
Assignees

Comments

@smkanadl
Copy link

With regards to #333, I understand that the supression does not work inside an Assert.Multiple block. But I would have expect the following scenario to work, which I typically use for such cases.

var e = Assert.Throws<Exception>(() => throw new Exception("Test"));
Assert.Multiple(() =>
{
    Assert.That(e.Message, Is.EqualTo("Test'")); // I see a warning here that e might be null
    Assert.That(e.InnerException, Is.Null);
    Assert.That(e.StackTrace, Is.Not.Empty);
});

If I remove the Assert.Multiple block, the warning is correctly suppressed.

var e = Assert.Throws<Exception>(() => throw new Exception("Test"));
Assert.That(e.Message, Is.EqualTo("Test'")); // no warning now
Assert.That(e.InnerException, Is.Null);
Assert.That(e.StackTrace, Is.Not.Empty);

Is this some limitation that could be removed?
I am using the 3.0 Version in Visual Studio 2019 Preview 16.9

@mikkelbu
Copy link
Member

Without looking at the code I think we should be able to determine that the assignment happens outside the Assert.Multiple, but I'm unsure how much this will complicate the code.

@mikkelbu mikkelbu added this to the Release 3.1 / 2.1 milestone Jan 19, 2021
@manfred-brands
Copy link
Member

Roslyn raises the 'possible null reference exception' on the line Assert.That(e.Message, Is.EqualTo("Test'"));.
The code then determines that this line is inside the Assert.Multiple and stops any search for the assignment and hence doesn't suppress. I will see if I can update the code to search for the assignment regardless.
@mikkelbu Please assign the ticket to me.

@smkanadl
Copy link
Author

@manfred-brands that would be great if it could be improved that way. Thanks for taking care!

@mikkelbu
Copy link
Member

Thanks @manfred-brands 👍

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

Successfully merging a pull request may close this issue.

3 participants