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
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:
Which, unfortunately, doesn't work for me.
Reproduction Steps
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.
Regression?
Don't know.
Known Workarounds
In the same issue, @donotcodeit gave a workaround (adapted to my repro example):
Configuration
Other information
No response