Skip to content

Assert.EnterMultipleScope not detecting nullability properly #892

@artificialWave

Description

@artificialWave

I've been rewriting all our tests that use Assert.Multiple to Assert.EnterMultipleScope but came across this issue that makes it currently impossible.

Given the following old code:

var restResponse = new RestResponse();

Assert.Multiple(() =>
{
    Assert.That(restResponse.StatusCode, Is.EqualTo(HttpStatusCode.OK));
    Assert.That(restResponse.Content, Is.Not.Null);
}

// restResponse.Content is definitely not null here
Assert.That(restResponse.Content.ToString(), Is.EqualTo("Test"));

And the code with EnterMultipleScope:

var restResponse = new RestResponse();

using (Assert.EnterMultipleScope())
{
    Assert.That(restResponse.StatusCode, Is.EqualTo(HttpStatusCode.OK));
    Assert.That(restResponse.Content, Is.Not.Null);
}

// restResponse.Content may be null here
Assert.That(restResponse.Content.ToString(), Is.EqualTo("Test"));

In the old version, the last assert does not produce a warning, because the analyzer knows that restResponse.Content is not null, or it would have failed earlier. In the newer version, it does produce a warning because it apparently does not know that restResponse.Content is definitely not null.

Both should work the same in detecting whether something can be null further on in the test.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions