Skip to content

Commit

Permalink
Fix ExcludingMissingMembers from reverting WithMapping usages (#1838)
Browse files Browse the repository at this point in the history
  • Loading branch information
dennisdoomen committed Mar 10, 2022
1 parent 6005b16 commit ebea08e
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 11 deletions.
Expand Up @@ -376,12 +376,12 @@ public TSelf Including(Expression<Func<IMemberInfo, bool>> predicate)
}

/// <summary>
/// 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.
/// </summary>
public TSelf ExcludingMissingMembers()
{
ClearMatchingRules();
matchingRules.RemoveAll(x => x is MustMatchByNameRule);
matchingRules.Add(new TryMatchByNameRule());
return (TSelf)this;
}
Expand All @@ -391,7 +391,7 @@ public TSelf ExcludingMissingMembers()
/// </summary>
public TSelf ThrowingOnMissingMembers()
{
ClearMatchingRules();
matchingRules.RemoveAll(x => x is TryMatchByNameRule);
matchingRules.Add(new MustMatchByNameRule());
return (TSelf)this;
}
Expand Down Expand Up @@ -463,7 +463,7 @@ public void WithoutSelectionRules()
/// </summary>
public void WithoutMatchingRules()
{
ClearMatchingRules();
matchingRules.Clear();
}

/// <summary>
Expand Down Expand Up @@ -844,12 +844,7 @@ private void RemoveSelectionRule<T>()
{
selectionRules.RemoveAll(selectionRule => selectionRule is T);
}

private void ClearMatchingRules()
{
matchingRules.Clear();
}


protected TSelf AddSelectionRule(IMemberSelectionRule selectionRule)
{
selectionRules.Add(selectionRule);
Expand Down
46 changes: 46 additions & 0 deletions Tests/FluentAssertions.Equivalency.Specs/MemberMatchingSpecs.cs
Expand Up @@ -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; }
Expand Down
1 change: 1 addition & 0 deletions docs/_pages/releases.md
Expand Up @@ -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)

Expand Down

0 comments on commit ebea08e

Please sign in to comment.