Skip to content

record type with List or IEnumerable property fails ShouldBeEquivalentTo #1507

@rcollette

Description

@rcollette

Before you file a bug, have you:

Description

A record with a List or IEnumerable property fails with BeEquivalentTo

Complete minimal example reproducing the issue

using System.Collections.Generic;
using FluentAssertions;
using Xunit;

namespace fluent_assertion_record_issue
{

    internal record ObjectWithList
    {
        public List<string> Prop { get; init; }
    }


    public class UnitTest1
    {

        [Fact]
        public void Test1()
        {
            var obj = new ObjectWithList()
            {
                Prop = new List<string>()
                {
                    "a", "b", "c"
                }
            };
            var obj2 = new ObjectWithList()
            {
                Prop = new List<string>()
                {
                    "a", "b", "c"
                }
            };

            // assert
            // fails
            obj2.Should().BeEquivalentTo(obj,opts=>opts.WithTracing());
        }
    }
}

Expected behavior:

Should succeed without error.

Actual behavior:

fluent_assertion_record_issue.UnitTest1.Test1

Expected obj2 to be ObjectWithList { Prop = System.Collections.Generic.List`1[System.String] }, but found ObjectWithList { Prop = System.Collections.G...

Xunit.Sdk.XunitException
Expected obj2 to be ObjectWithList { Prop = System.Collections.Generic.List`1[System.String] }, but found ObjectWithList { Prop = System.Collections.Generic.List`1[System.String] }.

With configuration:
- Use declared types and members
- Compare enums by value
- Match member by name (or throw)
- Without automatic conversion.
- Be strict about the order of items in byte arrays

With trace:
  Treating root as a value type because Equals must be used.

   at FluentAssertions.Execution.XUnit2TestFramework.Throw(String message)
   at FluentAssertions.Execution.TestFrameworkProvider.Throw(String message)
   at FluentAssertions.Execution.CollectingAssertionStrategy.ThrowIfAny(IDictionary`2 context)
   at FluentAssertions.Equivalency.EquivalencyValidator.AssertEquality(EquivalencyValidationContext context)
   at FluentAssertions.Primitives.ObjectAssertions.BeEquivalentTo[TExpectation](TExpectation expectation, Func`2 config, String because, Object[] becauseArgs)
   at fluent_assertion_record_issue.UnitTest1.Test1() in /Users/richardcollette/temp/fluent-assertion-record-issue/UnitTest1.cs:line 37

Versions

FluentAssertions 5.10.3

.NET SDK (reflecting any global.json):
Version: 5.0.201
Commit: a09bd5c86c

Runtime Environment:
OS Name: Mac OS X
OS Version: 10.15
OS Platform: Darwin
RID: osx.10.15-x64

Microsoft.AspNetCore.App 5.0.4
Microsoft.NETCore.App 5.0.4

Additional Information

If the record type in the example is replaced with class, then it does succeed. The issue appears to be specific to record types.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions