BeEquivalentTo fails when comparing two JsonElement's
See original GitHub issueTest is failing when comparing two Json Elements using ShouldBeEquivalentTo
. I’m sure that this Assertion
[Fact]
public void Test()
{
// Arrange & Act
var element1 = JsonDocument.Parse("{}").RootElement;
var element2 = JsonDocument.Parse("{}").RootElement;
// Assert
element1.Should().BeEquivalentTo(element2);
}
Expected behavior:
I would expect this to pass
Actual behavior:
This actually fails
Versions
XUnit v2.4.1 FluentAssertions v5.9.0 .NET Core 3.0
Issue Analytics
- State:
- Created 4 years ago
- Comments:8 (5 by maintainers)
Top Results From Across the Web
FluentAssertions Should().BeEquivalentTo() fails in trivial ...
I'm trying to do the simplest thing imaginable: compare the properties of an Address object with the properties of an AddressDto object. They ......
Read more >Just Use FluentAssertions - Tinkerer
BeEquivalentTo is the real killer feature of FluentAssertions for me. It allows you to compare whether or not two object have equivalent fields....
Read more >Object graph comparison
Imagine you want to compare an Order and an OrderDto using BeEquivalentTo , but the first type has a Name property and the...
Read more >Comparing objects with disparate members in Fluent Assertions
But up to now, it did not know how to compare two objects with misnamed properties or fields. ... BeEquivalentTo(rootExpectation, options => options...
Read more >The Type Hierarchy — SQLAlchemy 2.0 Documentation
Given two values, compare them for equality. By default this calls upon TypeEngine.compare_values() of the underlying “impl”, which in turn usually uses the ......
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
@dennisdoomen
JsonElement
is fromSystem.Text.Json
notNewtonsoft.Json
.Analysis:
JsonElement
is astruct
that does not overrideObject.Equals(object)
. That is, it keeps the default generatedValueType.Equals(object)
which compares instances memberwise using theirEquals(object)
.JsonElement
has a field_parent
, which points to its parentJsonDocument
.JsonDocument
also does not overrideObject.Equals(object)
, so instances are compared by reference.As the
_parent
fields ofelement1
andelement2
points to two differentJsonDocument
s in memory,element1
does not equalelement2
.This behavior can be seen with
Workaround: If you use
ComparingByMembers()
Fluent Assertions will compare object structurally (think memberwise), but per default only for public members. So it will now exclude_parent
.