FluentAssertions Should.Equal for collections containing zeros

FluentAssertions seems to fail with a NullReferece exception when I try to compare two collections with zeros

    [Test]
    public void DeepWithNulls()
    {
        var l1 = new List<string> { "aaa", null };
        var l2 = new List<string> { "aaa", null };

        l1.Should().Equal(l2);
    }

The comparison works as expected in collections without zeros.

+5
source share
1 answer

This is because, in the depths of the comparison logic of the collection, the Fluent Assertion uses the following code

 for (int index = 0; index < expectedItems.Length; index++)
            {
                verification.ForCondition((index < actualItems.Length) && actualItems[index].Equals(expectedItems[index]))
                    .FailWith("Expected " + Verification.SubjectNameOr("collection") +
                        " to be equal to {0}{reason}, but {1} differs at index {2}.", expected, Subject, index);
            }

in the code above expectedItemsand actualItems- your lists

Now think about what will happen during the second iteration, when (part below) is done?

actualItems[index].Equals(expectedItems[index])

which actualItems[1]is nullwhy it throws a null reference exception

+4
source

All Articles