Using Should().Equal() on two List<object> fails even though list contents are identical
See original GitHub issueDescription
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<int> as List<object> 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
Issue Analytics
- State:
- Created 3 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
JUnit test failing but showing identical Expected and Actual ...
So you're using the method assertEquals(Object expected, Object actual);. Obviously a String is not equals to a List<Object> and hence the fail.
Read more >Assert Two Lists for Equality Ignoring Order in Java
As per the List#equals Java documentation, two lists are equal if they contain the same elements in the same order.
Read more >Python's list Data Type: A Deep Dive With Examples
In this tutorial, you'll dive deep into Python's lists. You'll learn how to create them, update their content, populate and grow them, ...
Read more >The Difference Between “is” and “==” in Python – dbader.org
Python has two operators for equality comparisons, “is” and “==” (equals). In this article I'm going to teach you the difference between the...
Read more >List (Java Platform SE 8 )
Compares the specified object with this list for equality. Returns true if and only if the specified object is also a list, both...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
It’s an combination of an suboptimal API and compiler behavior. You’re using the
Equal
method that takes aparams
array. Because of that, you’re actually comparing a collection with a collection of collections of typeobject
. The double braces on the expectation part of the message should be the hint.@jnyrup it’s done. Thank you very much! 😃