Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BeEquivalentTo For().Exclude() does not work for complex type hierarchies #1986

Closed
marcin-dardzinski opened this issue Sep 5, 2022 · 3 comments
Labels

Comments

@marcin-dardzinski
Copy link

Description

I have a case where a base class is generic and the generic argument should be the class deriving from it. I'm trying to compare an object graph and exclude one property from this type, from a collection, i.e.
ShouldBeEquvalentTo(..., o => o.For(a => a.BS).Exclude(b => b.X.Y)
X is my type and Y is a property I want to exclude.
Note that if I have just one object and not the collection, it works fine.

Complete minimal example reproducing the issue

using System;
using FluentAssertions;
using Xunit;

namespace Tests
{
    public class FluentAssertionsTests
    {
        [Fact]
        public void Should_exclude_property()
        {
            var d1 = new Derived
            {
                Value = 5,
                LastModified = new DateTime(2022, 9, 5, 8, 0, 0),
            };

            var d2 = new Derived
            {
                Value = 5,
                LastModified = new DateTime(2022, 9, 5, 9, 0, 0),
            };

            d1.Should().BeEquivalentTo(d2, opts => opts.Excluding(d => d.LastModified));

            var foo = new Foo
            {
                Bars = new Bar[]
                {
                    new()
                    {
                        X = 1,
                        Y = d1,
                    },
                },
            };

            foo.Should()
                .BeEquivalentTo(
                    new
                    {
                        Bars = new[]
                        {
                            new
                            {
                                X = 1,
                                Y = d2,
                            },
                        },
                    },
                    opts => opts.For(x => x.Bars).Exclude(b => b.Y.LastModified));
        }

        public abstract class Base<TSelf>
            where TSelf : Base<TSelf>
        {
            public DateTime LastModified { get; set; }
        }

        public class Derived : Base<Derived>
        {
            public int Value { get; set; }
        }

        internal class Foo
        {
            public Bar[]? Bars { get; set; } = default!;
        }

        internal class Bar
        {
            public int X { get; set; }
            public Derived Y { get; set; } = default!;
        }
    }
}

Expected behavior:

The test passes, objects are equivalent except for foo.Bars[0].Y.LastModified.

Actual behavior:

The test fails with the following error:

Expected property foo.Bars[0].Y.LastModified to be <2022-09-05 09:00:00>, but found <2022-09-05 08:00:00>.

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
- Exclude member Bars[]Y.LastModified
- Match member by name (or throw)
- Be strict about the order of items in byte arrays
- Without automatic conversion.

Versions

FluentAssertions: 6.7.0
.NET 6.0 (sdk: 6.0.400, runtime: 6.0.8)

@marcin-dardzinski marcin-dardzinski changed the title BeEquivalentTo For().Exclude does not work for complex type hierarchies BeEquivalentTo For().Exclude() does not work for complex type hierarchies Sep 5, 2022
@dennisdoomen
Copy link
Member

@whymatter could this be caused by the same problem as is mentioned in #1919 ?

@whymatter
Copy link
Contributor

Sorry I missed the @ guys ...
We can close this as it works since 6.8.0 (this fixed it: #1953)
@dennisdoomen @marcin-dardzinski

@jnyrup
Copy link
Member

jnyrup commented Apr 7, 2023

I can confirm that the included test fails with 6.7.0 and passes with 6.8.0

@jnyrup jnyrup closed this as completed Apr 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants