Skip to content

BeEquivalentTo comparision When deep object and WithMapping #2812

@jgarciadelanoceda

Description

@jgarciadelanoceda

Description

I have two objects that are similar and I apply a BeEquivalentTo for members comparison. But the problem is that one property that is inside a WithMapping property is not present in the destination object.

I have finally to call Excluding((IMemberInfo ctx)) for that property

Reproduction Steps

using FluentAssertions;
using FluentAssertions.Equivalency;
using Xunit;

namespace NameSpace;

public class MappingTest
{
    public class Customer
    {
        public Residence Address { get; set; }
    }

    public class Residence
    {
        public string Address { get; set; }
    }

    public class CustomerDto
    {
        public ResidenceDto AddressInformation { get; set; }
    }

    public class ResidenceDto
    {
        public string Address { get; set; }

        public bool IsValidated { get; set; }
    }

    [Fact]
    public void CustomerDto_To_Customer_ShouldBeEquivalent()
    {
        // Arrange
        var req = new CustomerDto
        {
            AddressInformation = new ResidenceDto
            {
                Address = "foo",
                IsValidated = true
            }
        };
        
        // Act
        var res = new Customer
        {
            Address = new Residence()
            {
                Address = "foo"
            }
        };
        
        // Assert
        res.Should().BeEquivalentTo(req, o => o
        .WithMapping<CustomerDto, Customer>(s => s.AddressInformation, d => d.Address)
        //.Excluding((IMemberInfo ctx) => ctx.DeclaringType.IsAssignableFrom(typeof(ResidenceDto)) && ctx.Name == nameof(ResidenceDto.IsValidated))
        .Excluding(r => r.AddressInformation.IsValidated));
    }
}

Expected behavior

The simple Excluding(r => r.AddressInformation.IsValidated) should work

Actual behavior

Expectation has property res.Address.IsValidated that the other object does not have

Regression?

No response

Known Workarounds

The use of Excluding((IMemberInfo ctx))

Configuration

No response

Other information

No response

Are you willing to help with a pull-request?

No

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions