Note: For Assert, using Xunit
Anonymous objects use value equality:
Assert.Equal(
new { foo = "bar" },
new { foo = "bar" }
);
Anonymous arrays with anonymous objects use value equality:
Assert.Equal(
new [] { new { foo = "bar" } },
new [] { new { foo = "bar" } }
);
Anonymous objects with nested arrays seem to use reference equality (this fails):
Assert.Equal(
new { baz = new [] { new { foo = "bar" } },
new { baz = new [] { new { foo = "bar" } }
);
But this works (presumably because there's now reference equality):
var baz = new [] { new { foo = "bar" } };
Assert.Equal(
new { baz },
new { baz }
);
And this works (so it appears I can recursively nest anonymous objects with value equality preserved):
Assert.Equal(
new { baz = new { qux = new { foo = "bar" } } },
new { baz = new { qux = new { foo = "bar" } } }
);
I'm not sure how equality is being conducted here, but I'd like to be able to nest objects and arrays and have infinite-depth value equality. How might I go about doing that?
Update
Looks like Fluent Assertions solved my problem (this test passes):
(new { baz = new[] { new { foo = "bar" } } })
.Should()
.BeEquivalentTo(new { baz = new [] { new { foo = "bar" } } });