-
Notifications
You must be signed in to change notification settings - Fork 732
Generic collections .ContainEquivalentOf() doesn't respect supplied IEqualityComparer. #2283
Copy link
Copy link
Closed
Labels
Description
Description
Supplying a IEqualityComparer for generic collection comparison .ContainEquivalentOf() doesn't work as expected.
The supplied comparer is not used in the comparison.
Reproduction Steps
[Fact]
public void When_injecting_a_EqualityComparer_to_ContainEquivalentOf_it_should_be_used()
{
var allItems = Enumerable.Range(0, 10).Select(x => new CustomClass(x)).ToList();
var subset = Enumerable.Range(3, 3).Select(x => new CustomClass(x)).ToList();
// This works.
var matchingItems = allItems.Intersect(subset, new SimpleCustomClassComparer()).ToList();
matchingItems.Select(x => x.Id).Should().BeEquivalentTo(new[] { 3, 4, 5 });
// This doesn't work because the EqualityComparer isn't called by FluentAssertions.
allItems.Should().ContainEquivalentOf(subset, opt => opt.Using(new SimpleCustomClassComparer()));
}
private class CustomClass
{
public CustomClass(int id)
{
this.Id = id;
}
public int Id { get; set; }
public Guid Guid => Guid.NewGuid();
}
private class SimpleCustomClassComparer : IEqualityComparer<CustomClass>
{
public bool Equals(CustomClass x, CustomClass y)
{
return x?.Id == y?.Id;
}
public int GetHashCode(CustomClass obj)
{
return obj.Id;
}
}Expected behavior
This statement should pass:
allItems.Should().ContainEquivalentOf(subset, opt => opt.Using(new SimpleCustomClassComparer()));Actual behavior
The assertion fails because the supplied comparer is not used in the comparison.
Regression?
No response
Known Workarounds
// Workaround: Eliminate the need for a custom comparer
var allItemsIds = allItems.Select(x => x.Id);
var subsetIds = subset.Select(x => x.Id);
allItemsIds.Should().Contain(subsetIds);Configuration
No response
Other information
EqualityComparerEquivalencyStep Doesn't support being used in IEnumerable<T> scenarios.
Could you help with a pull-request?
No
Reactions are currently unavailable