ComparingByMembers<object> lets structural equality assertions always succeed
See original GitHub issueDescription
I want to be able to configure FluentAssertions’ structural equality so that it uses ComparingByMembers for all types.
The background for this is that I have a mix of F# and C# code and all F# records implement Equals by default. In case of an object-graph with a mix starts using Equals
when hitting the first F# record, as well for the nested C# classes.
I tried ComparingByMembers<object>
, but that results in the tests always being succeeded. And I guess that should be fixed.
Complete minimal example reproducing the issue
using FluentAssertions;
using Xunit;
namespace CSharp.FluentAssertionWrapper
{
public class Facts
{
public struct C
{
public D D { get; set; }
}
public class D
{
public string S { get; set; }
public int I { get; set; }
}
[Fact]
public void Foo()
{
var a = new C { D = new D { S = "a", I = 17 } };
var b = new C { D = new D { S = "a", I = 18 } };
a.Should().BeEquivalentTo( // this should fail, but succeeds
b,
config => config.ComparingByMembers<object>());
}
}
}
Expected behavior:
All types are structurally compared correctly.
Actual behavior:
Assertion always succeeds.
Versions
- Which version of Fluent Assertions are you using? 5.10.3
- Which .NET runtime and version are you targeting? netcoreapp3.1
Additional Information
None.
Issue Analytics
- State:
- Created 3 years ago
- Comments:22 (22 by maintainers)
Top Results From Across the Web
Structural equality for better tests - ploeh blog
With structural equality, you can express a relationship between the expected and actual outcome using high-level language.
Read more >Object graph comparison
You may assert the structural equality of two object graphs with Should(). ... equivalency comparisons, we decided to always compare them by their...
Read more >Chai assertions should have only one reason to succeed
Each assertion should test one condition and have only one reason to fail or succeed. If an assertion success depends on multiple conditions...
Read more >Making Better Unit Tests: part 1, the AAA pattern - Manning
How to structure a unit test: the Arrange-Act-Assert pattern ... it could be better to define proper equality members in the object's class....
Read more >Python's assert: Debug and Test Your Code Like a Pro
Comparison assertions are intended to test conditions that compare two or more objects using comparison operators. These assertions can also ...
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
I think that would work.
Should I try to contribute that? I probably need some help though. I don’t really understand the code around
SelfReferenceEquivalencyAssertionOptions
, which is somehow involved in the affected code.That’s what I was thinking,e.g.
ComparingByMembers(Func<Type, bool> predicate)