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 option .For().Exclude() not working with subobject's fields #1952

Closed
Smaxoth opened this issue Jul 4, 2022 · 3 comments · Fixed by #1953
Closed

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

Smaxoth opened this issue Jul 4, 2022 · 3 comments · Fixed by #1953
Labels

Comments

@Smaxoth
Copy link

Smaxoth commented Jul 4, 2022

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
@dennisdoomen
Copy link
Member

@whymatter do you think this should work out of the box?

@whymatter
Copy link
Contributor

@dennisdoomen Checked it, it does not work... Somehow the additional nesting in Exclude with Class3 causes a problem.

With version 6.7.0 .For().Exclude() just works if Exclude is used this way .Exclude(x => x.y) and not that way .Exclude(x => x.y.z)

However, I agree that wanting to use it like that .Exclude(x => x.y.z) is 100% justified. I'll take a look at this tonight

@whymatter
Copy link
Contributor

Okay I got something #1953

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants