question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

BeEquivalentTo fails when comparing two JsonElement's

See original GitHub issue

Test 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:closed
  • Created 4 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

4reactions
jnyrupcommented, Dec 3, 2020

@dennisdoomen JsonElement is from System.Text.Json not Newtonsoft.Json.

4reactions
jnyrupcommented, Dec 30, 2019

Analysis: JsonElement is a struct that does not override Object.Equals(object). That is, it keeps the default generated ValueType.Equals(object) which compares instances memberwise using their Equals(object).

JsonElement has a field _parent, which points to its parent JsonDocument. JsonDocument also does not override Object.Equals(object), so instances are compared by reference.

As the _parent fields of element1 and element2 points to two different JsonDocuments in memory, element1 does not equal element2.

This behavior can be seen with

element1.Equals(element2); // false

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.

element1.Should().BeEquivalentTo(element2, opt => opt.ComparingByMembers<JsonElement>());
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found