Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure ExcludingMissingMembers doesn't undo usage of WithMapping #1838

Merged
merged 1 commit into from Mar 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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