Skip to content

Using Should().Equal() on two List<object> fails even though list contents are identical #1346

@GeeWee

Description

@GeeWee

Description

Using Should().Equal() to compare two List<object> always fails, even though the items in the list are equal. I have reproduced below with strings, and confirmed it is also true with ints.

Complete minimal example reproducing the issue

A reproducible example below. The exact same test but the type of the list goes
from List<string> to List<object>

// Two identical strings - should succeed
var strings = new List<string>{"foo"};
var identicalStrings = new List<string>{"foo"};
strings.Should().BeEquivalentTo(identicalStrings); // Passes
strings.Should().Equal(identicalStrings); // Passes

// Same test, but here the specified type is List<object>
var objects = new List<object>{"bar"};
var identicalObjects = new List<object>{"bar"};
objects.Should().BeEquivalentTo(identicalObjects); // Passes
objects.Should().Equal(identicalObjects); // This fails with
// Expected objects to be equal to {{"bar"}}, but {"bar"} differs at index 0.

Full stack trace:

 Error Message:
   Expected objects to be equal to {{"bar"}}, but {"bar"} differs at index 0.
  Stack Trace:
     at FluentAssertions.Execution.XUnit2TestFramework.Throw(String message)
   at FluentAssertions.Execution.TestFrameworkProvider.Throw(String message)
   at FluentAssertions.Execution.DefaultAssertionStrategy.HandleFailure(String message)
   at FluentAssertions.Execution.AssertionScope.FailWith(Func`1 failReasonFunc)
   at FluentAssertions.Execution.AssertionScope.FailWith(Func`1 failReasonFunc)
   at FluentAssertions.Execution.AssertionScope.FailWith(String message, Object[] args)
   at FluentAssertions.Execution.GivenSelector`1.FailWith(String message, Object[] args)
   at FluentAssertions.Execution.GivenSelector`1.FailWith(String message, Func`2[] args)
   at FluentAssertions.Execution.GivenSelectorExtensions.AssertCollectionsHaveSameItems[TActual,TExpected](GivenSelector`1 givenSelector, ICollection`1 expected, Func`3 findIndex)
   at FluentAssertions.Collections.CollectionAssertions`2.AssertSubjectEquality[TActual,TExpected](IEnumerable expectation, Func`3 equalityComparison, String because, Object[] becauseArgs)
   at FluentAssertions.Collections.SelfReferencingCollectionAssertions`2.Equal(T[] elements)
   at AIP.FastlogFG.Test.FastlogReaderRegresssionTets.FOO() in /home/geewee/programming/OAI/FastlogFG/FastlogFG.Test/FastlogReaderRegressionTests.cs:line 42

I have confirmed the same happens with List as List where the error looks similar: Expected objects to be equal to {{2}}, but {2} differs at index 0.

Note that strangely enough this snippet passes succesfully:

var objects = new List<object> {"foo"};
objects.Should().Equal("foo"); 

So it seems to only happen when comparing two List<object>

Versions

Fluent Assertion 5.10.3
.NET Core 3.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions