Skip to content

BeEquivalentTo For().Exclude() does not work for comparison of collections #1989

@abartho

Description

@abartho

Description

I have an array or list of nested objects that I would like to check for equivalency. The child objects have some properties that I would like to exclude from the comparison. I'm using the new For().Exclude() syntax. That works fine when comparing individual objects, but it fails when comparing collections.

Complete minimal example reproducing the issue

using FluentAssertions;

public class ForExcludeTest
{
    [Fact]
    public void Test()
    {
        var actualContainer = new Container
        {
            Children = new[]
            {
                new Child
                {
                    ChildId = 20,
                    Name = "Child20"
                }
            }
        };

        var expectedContainer = new Container
        {
            Children = new[]
            {
                new Child
                {
                    ChildId = 20
                }
            }
        };

        // Comparing the actual objects works
        actualContainer.Should().BeEquivalentTo(expectedContainer,
            options => options
                .For(o => o.Children).Exclude(o => o.Name)
            );

        // Comparing the objects inside a list fails
        new List<Container> { actualContainer }.Should().BeEquivalentTo(new List<Container> { expectedContainer },
            options => options
                .For(o => o.Children).Exclude(o => o.Name)
            );
    }

    public class Container
    {
        public Child[] Children { get; set; }
    }

    public class Child
    {
        public int ChildId { get; set; }
        public string Name { get; set; }
    }
}

Expected behavior:

Both assertions should succeed.

Actual behavior:

The second assertion (on a list) fails with the following error:

Expected property root[0].Children[0].Name to be <null>, but found "Child20".

With configuration:
- Use declared types and members
- Compare enums by value
- Compare tuples by their properties
- Compare anonymous types by their properties
- Compare records by their members
- Include non-browsable members
- Include all non-private properties
- Include all non-private fields
- Exclude member Children[]Name
- Match member by name (or throw)
- Be strict about the order of items in byte arrays
- Without automatic conversion.


  Stack Trace: 
XUnit2TestFramework.Throw(String message)
TestFrameworkProvider.Throw(String message)
CollectingAssertionStrategy.ThrowIfAny(IDictionary`2 context)
EquivalencyValidator.AssertEquality(Comparands comparands, EquivalencyValidationContext context)
GenericCollectionAssertions`3.BeEquivalentTo[TExpectation](IEnumerable`1 expectation, Func`2 config, String because, Object[] becauseArgs)
ForExcludeTest.Test() line 38

Versions

  • FluentAssertions 6.7.0
  • .NET 6.0

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions