From ebea08eee37ae560b5da78408b1174e4ab478ae4 Mon Sep 17 00:00:00 2001 From: Dennis Doomen Date: Thu, 10 Mar 2022 20:52:08 +0100 Subject: [PATCH] Fix ExcludingMissingMembers from reverting WithMapping usages (#1838) --- ...elfReferenceEquivalencyAssertionOptions.cs | 17 +++---- .../MemberMatchingSpecs.cs | 46 +++++++++++++++++++ docs/_pages/releases.md | 1 + 3 files changed, 53 insertions(+), 11 deletions(-) diff --git a/Src/FluentAssertions/Equivalency/SelfReferenceEquivalencyAssertionOptions.cs b/Src/FluentAssertions/Equivalency/SelfReferenceEquivalencyAssertionOptions.cs index a528d156c0..e1415be062 100644 --- a/Src/FluentAssertions/Equivalency/SelfReferenceEquivalencyAssertionOptions.cs +++ b/Src/FluentAssertions/Equivalency/SelfReferenceEquivalencyAssertionOptions.cs @@ -376,12 +376,12 @@ public TSelf Including(Expression> predicate) } /// - /// Tries to match the members of the subject with equally named members on the expectation. Ignores those - /// members that don't exist on the expectation and previously registered matching rules. + /// Tries to match the members of the expectation with equally named members on the subject. Ignores those + /// members that don't exist on the subject and previously registered matching rules. /// public TSelf ExcludingMissingMembers() { - ClearMatchingRules(); + matchingRules.RemoveAll(x => x is MustMatchByNameRule); matchingRules.Add(new TryMatchByNameRule()); return (TSelf)this; } @@ -391,7 +391,7 @@ public TSelf ExcludingMissingMembers() /// public TSelf ThrowingOnMissingMembers() { - ClearMatchingRules(); + matchingRules.RemoveAll(x => x is TryMatchByNameRule); matchingRules.Add(new MustMatchByNameRule()); return (TSelf)this; } @@ -463,7 +463,7 @@ public void WithoutSelectionRules() /// public void WithoutMatchingRules() { - ClearMatchingRules(); + matchingRules.Clear(); } /// @@ -844,12 +844,7 @@ private void RemoveSelectionRule() { selectionRules.RemoveAll(selectionRule => selectionRule is T); } - - private void ClearMatchingRules() - { - matchingRules.Clear(); - } - + protected TSelf AddSelectionRule(IMemberSelectionRule selectionRule) { selectionRules.Add(selectionRule); diff --git a/Tests/FluentAssertions.Equivalency.Specs/MemberMatchingSpecs.cs b/Tests/FluentAssertions.Equivalency.Specs/MemberMatchingSpecs.cs index 990000bce1..2a813ea8ef 100644 --- a/Tests/FluentAssertions.Equivalency.Specs/MemberMatchingSpecs.cs +++ b/Tests/FluentAssertions.Equivalency.Specs/MemberMatchingSpecs.cs @@ -429,6 +429,52 @@ public void The_member_name_on_a_nested_type_mapping_must_be_a_valid_member() .WithMessage("*does not have member NonExistingProperty*"); } + [Fact] + public void Exclusion_of_missing_members_works_with_mapping() + { + // Arrange + var subject = new + { + Property1 = 1 + }; + + var expectation = new + { + Property2 = 2, + Ignore = 3 + }; + + // Act / Assert + subject.Should() + .NotBeEquivalentTo(expectation, opt => opt + .WithMapping("Property2", "Property1") + .ExcludingMissingMembers() + ); + } + + [Fact] + public void Mapping_works_with_exclusion_of_missing_members() + { + // Arrange + var subject = new + { + Property1 = 1 + }; + + var expectation = new + { + Property2 = 2, + Ignore = 3 + }; + + // Act / Assert + subject.Should() + .NotBeEquivalentTo(expectation, opt => opt + .ExcludingMissingMembers() + .WithMapping("Property2", "Property1") + ); + } + internal class ParentOfExpectationWithProperty2 { public ExpectationWithProperty2[] Parent { get; } diff --git a/docs/_pages/releases.md b/docs/_pages/releases.md index ca79d496b8..9cc588475f 100644 --- a/docs/_pages/releases.md +++ b/docs/_pages/releases.md @@ -15,6 +15,7 @@ sidebar: ### Fixes * `EnumAssertions.Be` did not determine the caller name - [#1835](https://github.com/fluentassertions/fluentassertions/pull/1835) +* Ensure `ExcludingMissingMembers` doesn't undo usage of `WithMapping` in `BeEquivalentTo` - [#1838](https://github.com/fluentassertions/fluentassertions/pull/1838) ### Fixes (Extensibility)