ShouldBeEquivalentTo ignores differences in deserialized objects #1785
-
DescriptionWhen comparing objects deserialized from JSON strings, differences are ignored. Complete minimal example reproducing the issueExample 1: var a = JsonConvert.DeserializeObject("{\"x\": 8}");
var b = JsonConvert.DeserializeObject("{\"x\": 7}");
a.Should().BeEquivalentTo(b); Example 2: var a = JsonConvert.DeserializeObject<JObject>("{\"x\": 8}");
var b = JsonConvert.DeserializeObject<JObject>("{\"x\": 7}");
a.Should().BeEquivalentTo(b); Expected behavior:In both of these examples, the two objects should be flagged as NOT equivalent. Actual behavior:The two objects are claimed to be equivalent. Versions
Additional InformationAny additional information, configuration or data that might be necessary to reproduce the issue. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
That's because you're comparing two instances of type |
Beta Was this translation helpful? Give feedback.
-
It's not that it doesn't support it. It's just that these types of objects have a complicated object graph structure with lots of members and cyclic references, and are not intended to be compared in such a way. So FA will still try to compare them by their members (depending or not if the objects in the graph override |
Beta Was this translation helpful? Give feedback.
That's because you're comparing two instances of type
object
, which doesn't define any compile-time properties. But even if you would use the options to force FA to respect the runtime types, the behavior is difficult to predict.JSonConvert
returns a complicated graph ofJContainer
instances. Instead, I recommend usingFluentAssertion.Json
. It also has aBeEquivalentTo
method that was specifically designed to work with Json objects. Just make sure you useusing FluentAssertions.Json
.