Skip to content

NRE when expectation is a Dictionary and subject is null #930

@jnyrup

Description

@jnyrup

Description

We have a bug, where a NullReferenceException is thrown if the expectation is a dictionary type and the subject is null.

For other enumerables GenericEnumerableEquivalencyStep.AssertSubjectIsCollection checks if the subject is not null.

There is no such check in GenericDictionaryEquivalencyStep and AssertIsCompatiblyTypedDictionary will throw an NRE on subject.GetType() when subject is null.

Complete minimal example reproducing the issue

[Fact]
public void When_a_dictionary_is_compared_to_null_it_should_not_throw_a_NullReferenceException()
{
    //-----------------------------------------------------------------------------------------------------------
    // Arrange
    //-----------------------------------------------------------------------------------------------------------
    Dictionary<int, int> subject = null;
    Dictionary<int, int> expectation = new Dictionary<int, int>();

    //-----------------------------------------------------------------------------------------------------------
    // Act
    //-----------------------------------------------------------------------------------------------------------
    Action act = () => subject.Should().BeEquivalentTo(expectation);

    //-----------------------------------------------------------------------------------------------------------
    // Assert
    //-----------------------------------------------------------------------------------------------------------
    act.Should().Throw<XunitException>();
}

Expected behavior:

The test should pass, as BeEquivalenTo should throw an XunitException.

Actual behavior:

The test fails as a NullReferenceException is thrown.

Versions

Fluent Assertions 5.4.2

Thanks to @SneManden for reporting

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