Skip to content

BeEquivalentTo does not exclude internal members #1205

@comecme

Description

@comecme

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);
        }
    }
}

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions