question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Excluding properties behind an array doesn't work in BeEquivalentTo on collections

See original GitHub issue

Description

A bit of a weird title but basically if I call myCollection.Should().BeEquivalent(expected, cfg => cfg.Excluding(i => i.SomeArray[0].SomeProperty)), the property isn’t correctly excluded and I get the follow assertion error:

Expected property root[0].Items[0].Name to be “Foo”, but “Bar” differs near “Bar”

Complete minimal example reproducing the issue

var expected = new A[] {
	new A { Items = new List<B> { new B { Name = "Foo" } } }
};

var actual = new A[] {
	new A { Items = new List<B> { new B { Name = "Bar" } } }
};

actual.Should().BeEquivalentTo(
	expected,
	cfg => cfg.Excluding(a => a.Items[0].Name)
);

class A
{
	public string Name { get; set; }
	public List<B> Items { get; set; }
}

class B
{
	public string Name { get; set; }
}

Expected behavior:

The excluding rule to be used and the test to pass.

Actual behavior:

The excluding rule is ignored and the test fails.

Versions

  • Which version of Fluent Assertions are you using? v6.6.0

  • Which .NET runtime and version are you targeting? E.g. .NET framework 4.6.1 or .NET Core 2.1. .NET 6

Additional Information

I believe this is a regression between v5.5.3 and v6.6.0 - I haven’t (yet) nailed down which version but will update when I have.

Edit: The regression starts in v6.0.0. The last v5 has this behaving correctly.

Additionally, this only seems to affect properties behind the array in the excluded check. Properties on the same level as the array work as expected if excluded.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

5reactions
dennisdoomencommented, May 6, 2022

I initially thought it worked given we have this successful test, but that one is not using a collection at the root. Also, I can confirm the behavior is slightly different when the root is a collection.

3reactions
IT-VBFKcommented, May 4, 2022

Ahhm… this should be fixed with the next release. See #1782

The new syntax will be:


actual.Should().BeEquivalentTo(
	expected,
	cfg => cfg
                 .For(a => a.Items)
                 .Exclude(b => b.Name));

Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - How to use Exclude in FluentAssertions for property in ...
This works for me: gotA.ShouldBeEquivalentTo(expectedB , config => config .Excluding(ctx => ctx.SelectedMemberInfo.MemberType == typeof(Venue)) ...
Read more >
[Solved]-How to use Exclude in FluentAssertions for property ...
The scenario I have is around testing Json serialization where I ignore certain properties. This works for me: gotA.ShouldBeEquivalentTo(expectedB , config => ...
Read more >
Excluding Child Properties from a List of Objects in Fluent ...
One issue we did have though was how to exclude specific child properties from a list property that belonged to our object being...
Read more >
Object graph comparison
Since object doesn't expose any properties, it makes no sense to respect the declared type. ... This will also work better with (multidimensional)...
Read more >
Ranges and indices - C# 8.0 draft feature specifications
This feature specification describes the syntax for ranges and indices, which support indexing individual elements of a sequence or a range ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found