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 + Excluding does not work with structs and classes overriding Equals

See original GitHub issue

FluentAssertions 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:closed
  • Created 5 years ago
  • Reactions:4
  • Comments:14 (6 by maintainers)

github_iconTop GitHub Comments

3reactions
Kikimoracommented, Oct 4, 2018

You making sound point, that types which override Equals should be compared with Equals. This unfortunately comes with inconsistency, since suddenly o => o.Excluding(x => x.Field1) does not work for structs for no apparent reason. Either exception should be thrown or assertions can switch to ComparingByMembers automatically when user request Excluding or similar option. I think that would make a better API and user experience.

2reactions
Kikimoracommented, Oct 4, 2018

I find this quite counterintuitive, I think ComparingByMembers should be on by default for structures.

Read more comments on GitHub >

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

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