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

CS8602/CS8604 odd behaviour flagging in unit tests despite null assertion. #7476

Open
StevePy opened this issue Nov 22, 2024 · 0 comments
Open

Comments

@StevePy
Copy link

StevePy commented Nov 22, 2024

For example the following code:

Assert.That(given.CapturedPaginationViewModel, Is.Not.Null);
Assert.That(given.CapturedPaginationViewModel.CurrentPage, Is.EqualTo(1)); // <- CS8602
Assert.That(given.CapturedPaginationViewModel.PageSize, Is.EqualTo(5));
Assert.That(given.CapturedPaginationViewModel.TotalRecords, Is.EqualTo(1));

where "given" is a BDD-style wrapper for the test:

private class Given : BaseGiven
{
    // ...
    public PaginationViewModel? CapturedPaginationViewModel { get; private set; } = default;
    // ...
}

The 2nd line, and only the 2nd line is getting flagged with a CS8602. With a .Net Core 8 build this warning did not appear, it seemed to pick up the Is.Not.Null assertion and was satisfied with that. What is quite unusual is that if I add a null forgiveness "!" to the second line, the warning does not migrate to the 3rd or 4th lines, they do not produce the CS8602 warning.

CurrentPage, PageSize, and TotalRecords are all int properties of the view model. I don't like the idea of slapping null forgiveness bangs in my unit tests, and I do not like having builds littered with warnings to ignore. Adding bangs to the unit tests is a lesser evil but I wanted to raise this just to determine if this is a unexpected issue that can be fixed, or is there a better way to test scenarios like this. (Assert.Multiple had no impact on the warning)

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

No branches or pull requests

1 participant