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.

ComparingByMembers<object> lets structural equality assertions always succeed

See original GitHub issue

Description

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:open
  • Created 3 years ago
  • Comments:22 (22 by maintainers)

github_iconTop GitHub Comments

1reaction
ursenzlercommented, Aug 12, 2020

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.

1reaction
dennisdoomencommented, Aug 12, 2020

That’s what I was thinking,e.g. ComparingByMembers(Func<Type, bool> predicate)

Read more comments on GitHub >

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

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