Creating a IEqualityComparer
I'm using xUnit and it doesn't have a way to determine if 2 IEnumerable<T>
are equal if T is custom type.
I've tried using LINQ SequenceEqual but again as the instances of T are
different this returns false;
Here is a basic test with a non-working IEqualityComparer
[Fact]
public void FactMethodName()
{
var one = new[] { new KeywordSchedule() { Id = 1 } };
var two = new[] { new KeywordSchedule() { Id = 1 } };
Assert.Equal(one, two, new KeywordScheduleComparer());
}
public class KeywordScheduleComparer :
IEqualityComparer<IEnumerable<KeywordSchedule>>
{
public bool Equals(IEnumerable<KeywordSchedule> x,
IEnumerable<KeywordSchedule> y)
{
return Object.ReferenceEquals(x, y) || (x != null && y != null &&
x.SequenceEqual(y));
}
public int GetHashCode(IEnumerable<KeywordSchedule> obj)
{
if (obj == null)
return 0;
return unchecked(obj.Select(e => e.GetHashCode()).Aggregate(0, (a,
b) => a + b)); // BAD
}
}
I'm using this in an integration test, so I insert data from a IEnumerable
into a DB at the start, then call my SUT to retrieve data from DB and
compare.
If you can help me get a collection comparison working I'd appreciate it!
No comments:
Post a Comment