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 does not exclude internal members

See original GitHub issue

BeEquivalentTo fails if an internal property has a different value.

The documentation, section “Including properties and/or fields” states:

Barring other configuration, Fluent Assertions will include all public properties and fields

However, it also seems to take into account all internal properties and fields.

Who is wrong? The documentation or the implementation? What is BeEquivalentTo supposed to do?

In the following code sample, PrivateMembersShouldNotBeCompared succeeds but InternalMembersShouldNotBeCompared fails on the compare of property InternalProperty.

using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace UnitTestProject
{
    [TestClass]
    public class TestBeEquivalentTo
    {
        class TestSubject
        {
            private string PrivateProperty { get; }
            internal string InternalProperty { get; }
            public string PublicProperty { get; }

            public TestSubject(string privateString, string internalString, string publicString)
            {
                PrivateProperty = privateString;
                InternalProperty = internalString;
                PublicProperty = publicString;
            }
        }

        [TestMethod]
        public void PrivateMembersShouldNotBeCompared()
        {
            TestSubject first = new TestSubject("A", "A", "A");
            TestSubject second = new TestSubject("B", "A", "A");

            first.Should().BeEquivalentTo(second);
        }

        [TestMethod]
        public void InternalMembersShouldNotBeCompared()
        {
            TestSubject first = new TestSubject("A", "A", "A");
            TestSubject second = new TestSubject("B", "B", "A");

            first.Should().BeEquivalentTo(second);
        }
    }
}

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
nielsheerencommented, May 16, 2021

I found a work-around that only checks public properties:

using FluentAssertions.Equivalency;
...
result.Should().BeEquivalentTo(expected, options => options
    .Including(info => info.WhichGetterHas(FluentAssertions.Common.CSharpAccessModifier.Public)));
1reaction
dennisdoomencommented, Aug 27, 2020

Time 😉

Read more comments on GitHub >

github_iconTop Results From Across the Web

FluentAssertions won't exclude missing members on object ...
I'm trying to exclude these extra properties with the ExcludingMissingMembers options and even explicitly by excluding each member by itself ...
Read more >
Object graph comparison
If you want to exclude certain (potentially deeply nested) individual members using the Excluding() method: orderDto.Should().BeEquivalentTo(order, ...
Read more >
Comparing objects with disparate members in Fluent Assertions
Imagine you want to assert that an OrderDto is equivalent to an Order entity using BeEquivalentTo. This is a pretty common thing to...
Read more >
Fluent Assertions 10 Top Tips
BeEquivalentTo() is very powerful and has plenty of options. Among its features, you can select properties and/or fields to include or exclude, ...
Read more >
Fluent Assertions: Fluently Assert the Result of .NET Tests
Selecting members. If you want, you can exclude some members using the Excluding option: personDto.Should ...
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