Skip to content

Potential NREs when using AssertionScope #3178

@jnyrup

Description

@jnyrup

Description

Searching for this pattern ForCondition\(([a-z]+) is not null\)\r\n(.*\r\n){1,11} +\.Then(\r\n *)?\.ForCondition\(\!?\1\!?\.
approximately reveals places on the form

.ForCondition(foo is not null)
...
.Then
.ForCondition(foo.)

When running the assertion method inside an AssertionScope, the AssertionChain doesn't stop on first error but continues until the surrounding AssertionScope is disposed.

So in the snippet above foo. might throw a NullReferenceException.

  • PropertyInfoAssertions
    • BeVirtual
    • NotBeVirtual
    • BeWritable
    • BeWritable(CSharpAccessModifier)
    • NotBeWritable
    • BeReadable
    • BeReadable(CSharpAccessModifier
    • NotBeReadable
    • Return
    • NotReturn
  • TypeAssertions
    • HaveProperty
    • HaveIndexer

Reproduction Steps

Edit the existing When_asserting_a_type_has_a_property_which_it_does_not_it_fails to wrap the call to HaveProperty in an AssertionScope

[Fact]
public void When_asserting_a_type_has_a_property_which_it_does_not_it_fails()
{
    // Arrange
    var type = typeof(ClassWithNoMembers);

    // Act
    Action act = () =>
    {
        using var _ = new AssertionScope();
        type.Should().HaveProperty(typeof(string), "PublicProperty", "we want to test the failure {0}", "message");
    };

    // Assert
    act.Should().Throw<XunitException>()
        .WithMessage("Expected ClassWithNoMembers to have a property PublicProperty of type String because we want to test the failure message, but it does not.");
}

Expected behavior

No NRE should be thrown

Actual behavior

An NRE is thrown

Regression?

No

Known Workarounds

No response

Configuration

No response

Other information

No response

Are you willing to help with a pull-request?

No

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions