Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

.For(x => x.[...]).Exclude(x => x.[...]) doesn't exclude properties for nested collections #2177

Copy link
Copy link

Description

@JPtenBerge
Issue body actions

Description

I have a collection of objects with nested collections. I'm trying to exclude certain properties from the nested collections. Basically, I'm trying to do exactly what @dennisdoomen mentioned here:

orderDto.ShouldBeEquivalentTo(order, options => options.Excluding(o => o.Products[Something.AllItems].Status));

You can now do

orderDto.Should().BeEquivalentTo(order,
    options => options
        .For(x => x.Products)
        .Exclude(x => x.Status));

Which, unfortunately, doesn't work for me.

Reproduction Steps

using Newtonsoft.Json;

namespace FluentAssertionsTests;

class Foo
{
    public string Name { get; set; }
    public List<Bar> Bars { get; set; }
}

class Bar
{
    public int Id { get; set; }
    public string Title { get; set; }
}

[TestClass]
public class FluentAssertionsCollectionExclusionTests
{
    [TestMethod]
    public void FluentAssertionCollectionExclusion()
    {
        var expected = new List<Foo>
        {
            new Foo
            {
                Bars = new()
                {
                    new() { Id = 4, Title = "first" },
                    new() { Id = 8, Title = "second" }
                }
            },
            new Foo
            {
                Bars = new()
                {
                    new() { Id = 8, Title = "third" }
                }
            }
        };

        // Newtonsoft/System.Text.Json doesn't matter, just looking for a quick deep clone
        var json = JsonConvert.SerializeObject(expected);
        var actual = JsonConvert.DeserializeObject<Foo[]>(json)!;
        // var json = System.Text.Json.JsonSerializer.Serialize(expected);
        // var actual = System.Text.Json.JsonSerializer.Deserialize<Foo[]>(json);

        // change prop to be excluded
        expected.First().Bars.Last().Title = "whoops";

        // does not work: Expected property actual[0].Bars[1].Title to be "whoops", but "second" differs near "sec" (index 0).
        actual.Should().BeEquivalentTo(expected, options => options.For(x => x.Bars).Exclude(x => x.Title));
    }
}

Expected behavior

That the property of the nested collection is excluded for assertion.

Actual behavior

It's not excluded during assertion and my test fails as a result.

Expected property actual[0].Bars[1].Title to be "whoops", but "second" differs near "sec" (index 0).

Regression?

Don't know.

Known Workarounds

In the same issue, @donotcodeit gave a workaround (adapted to my repro example):

actual.Should().BeEquivalentTo(expected, options =>
{
    options.Using<Bar>(ctx => ctx.Subject.Should().BeEquivalentTo(ctx.Expectation, barOptions =>
        barOptions.Excluding(q => q.Title)
    )).WhenTypeIs<Bar>();

    return options;
});

Configuration

  • Targeting .NET 7.
  • Using the latest FluentAssertions, v6.10.0

Other information

No response

Reactions are currently unavailable

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

    Morty Proxy This is a proxified and sanitized view of the page, visit original site.