Skip to content

Excluding not functioning on overridden covariant property #1562

@taylorn16

Description

@taylorn16

Description

Excluding not functioning on overridden covariant property.

See #1077 (comment) for original context.

Complete minimal example reproducing the issue

using FluentAssertions;

public class BaseProp
{
    public string BaseVal { get; set; }
}

public class DerivedProp : BaseProp
{
    public string DerivedVal { get; set; }
}

public abstract class Base
{
    public abstract BaseProp Prop { get; }
}

public sealed class Derived : Base
{
    public override DerivedProp Prop { get; }
    public string OtherProp { get; set; }
    
    public Derived(DerivedProp prop)
    {
        Prop = prop;
    }
}

public class Program
{
    public static void Main()
    {
        var a = new Derived(new DerivedProp { DerivedVal = "a", BaseVal = "a_base" })
        {
            OtherProp = "other"
        };
        var b = new Derived(new DerivedProp { DerivedVal = "b", BaseVal = "b_base" })
        {
	        OtherProp = "other"
        };
        
        a.Should().BeEquivalentTo(b, opts => opts
                                            .ComparingByMembers<Derived>()
                                            .Excluding(d => d.Prop));
    }
}

Expected behavior:

The .Should().BeEquivalentTo call should result in no test failure exception being thrown.

Actual behavior:

Unhandled exception. FluentAssertions.Execution.AssertionFailedException: Expected member Prop.BaseVal to be "b_base", but "a_base" differs near "a_b" (index 0).

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

   at FluentAssertions.Execution.FallbackTestFramework.Throw(String message) in C:\projects\fluentassertions-vf06b\Src\FluentAssertions\Execution\FallbackTestFramework.cs:line 18
   at FluentAssertions.Execution.TestFrameworkProvider.Throw(String message) in C:\projects\fluentassertions-vf06b\Src\FluentAssertions\Execution\TestFrameworkProvider.cs:line 40
   at FluentAssertions.Execution.CollectingAssertionStrategy.ThrowIfAny(IDictionary`2 context) in C:\projects\fluentassertions-vf06b\Src\FluentAssertions\Execution\CollectingAssertionStrategy.cs:line 44
   at FluentAssertions.Equivalency.EquivalencyValidator.AssertEquality(EquivalencyValidationContext context) in C:\projects\fluentassertions-vf06b\Src\FluentAssertions\Equivalency\EquivalencyValidator.cs:line 43
   at FluentAssertions.Primitives.ObjectAssertions.BeEquivalentTo[TExpectation](TExpectation expectation, Func`2 config, String because, Object[] becauseArgs) in C:\projects\fluentassertions-vf06b\Src\FluentAssertions\Primitives\ObjectAssertions.cs:line 125
   at Program.Main()
Command terminated by signal 6

Versions

  • FluentAssertions 5.10.3
  • .NET 5

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions