BeEquivalentTo + Excluding does not work with structs and classes overriding Equals
See original GitHub issueFluentAssertions 5.4.2, .net 2.1
tt1.Should().BeEquivalentTo(tt2, o => o.Excluding(x => x.Field1));
does not work for struct
. See test case below that demonstrate it works for class with fields but does not work for structs with fields.
public class FF
{
public string Field1;
public string Field2;
}
public struct SS
{
public string Field1;
public string Field2;
}
[Test]
public void FFT()
{
var tt1 = new FF() {Field1 = "1", Field2 = "Same"};
var tt2 = new FF() {Field1 = "2", Field2 = "Same"};
tt1.Should().BeEquivalentTo(tt2, o => o.Excluding(x => x.Field1));
}
[Test]
public void SST()
{
var tt1 = new SS() {Field1 = "1", Field2 = "Same"};
var tt2 = new SS() {Field1 = "2", Field2 = "Same"};
tt1.Should().BeEquivalentTo(tt2, o => o.Excluding(x => x.Field1));
}
Issue Analytics
- State:
- Created 5 years ago
- Reactions:4
- Comments:14 (6 by maintainers)
Top Results From Across the Web
c# - How to use Exclude in FluentAssertions for property in ...
I want to use fluent assertions to compare to ClassA instances. However I want to ignore the IDs (because the IDs will have...
Read more >Object graph comparison
Equals as an object that was designed to have value semantics. Anonymous types, record s, record struct s and tuples also override this...
Read more >[Solved]-How to use Exclude in FluentAssertions for property ...
Excluding(x => x.CreationTimestamp)). BUT if you work with structs and class overriding equals, then you should change the default comparing with ...
Read more >Releases - Fluent Assertions
FixesPermalink. Excluding with BeEquivalentTo did not always work well with overridden properties - #1087 & #1077; Failure message formatting threw ...
Read more >How to define value equality for a class or struct - C# ...
Any struct that you define already has a default implementation of value equality that it inherits from the System. ValueType override of 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
You making sound point, that types which override
Equals
should be compared withEquals
. This unfortunately comes with inconsistency, since suddenlyo => o.Excluding(x => x.Field1)
does not work for structs for no apparent reason. Either exception should be thrown or assertions can switch toComparingByMembers
automatically when user requestExcluding
or similar option. I think that would make a better API and user experience.I find this quite counterintuitive, I think
ComparingByMembers
should be on by default for structures.