Skip to content

BeEquivalentTo option .For().Exclude() not working with subobject's fields #1952

@Smaxoth

Description

@Smaxoth

Description

When using .For().Exclude() as a BeEquivalentTo() option, subobject's fields are not excluded properly.

Complete minimal example reproducing the issue

using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;

namespace ForExcludeBug
{
    public class Class1 { public Class2[] objs; }
    public class Class2 { public Class3 subobj; }
    public class Class3 { public string foo; }

    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void TestMethod1()
        {
            var expected = new Class1 { objs = new Class2[] { new Class2 { subobj = new Class3 { foo = "foo"} } } };
            var sut = new Class1 { objs = new Class2[] { new Class2 { subobj = new Class3 { foo = "bar" } } } };

            sut.Should().BeEquivalentTo(expected, options => options.For(x => x.objs).Exclude(x => x.subobj.foo));

        }

        [TestMethod]
        public void OKExample()
        {
            var expected = new Class1 { objs = new Class2[] { new Class2 { subobj = new Class3 { foo = "foo" } } } };
            var sut = new Class1 { objs = new Class2[] { new Class2 { subobj = new Class3 { foo = "bar" } } } };

            sut.Should().BeEquivalentTo(expected, options => options.Excluding(x => x.objs[0].subobj.foo));

        }
    }
}

Expected behavior:

Subobjects foo should be excluded, as in the excluding example.

Actual behavior:

Test fails; test report still mentions foo being excluded

 Message: 
Expected field sut.objs[0].subobj.foo to be "foo", but "bar" differs near "bar" (index 0).

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

Versions

  • 6.7.0
  • .NET Framework 4.8

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