If you take a look at the implementation of the NUnit equality comparer in the GIT repo, you will see that there is a dedicated comparison block for two enumerations, which has a higher priority (simply because it is placed higher) than the comparisons using the IEquatable<T>
interface or the Object.Equals(Object)
method, which you have implemented or overloaded in your PagedModel
class.
I don't know if this is a bug or a feature, but you probably should ask yourself first, if implementing the IEnumerable<ModelData>
interface directly by your PagedModel
class is actually the best option, especially because your PagedModel
is something more than just an enumeration of ModelData
instances.
Probably it would be enough (or even better) to provide the ModelData
enumeration via a simple read-only IEnumerable<ModelData>
property of the PagedModel
class. NUnit would stop looking at your PagedModel
object as at a simple enumeration of ModelData
objects and your unit tests would behave as expected.
The only other option is the one suggested by csauve; to implement a simple custom IComparer
for your PagedModel
and to supply an instance of it to all asserts where you will compare two PagedModel
instances:
internal class PagedModelComparer : System.Collections.IComparer
{
public static readonly IComparer Instance = new PagedModelComparer();
private PagedModelComparer()
{
}
public int Compare( object x, object y )
{
return x is PagedModel && ((PagedModel)x).Equals( y );
}
}
...
[Test]
...
Assert.That( actual, Is.EqualTo( expected ).Using( PagedModelComparer.Instance ) );
...
But this will make your tests more complicated than necessary and you will always have to think to use your special comparer whenever you are writing additional tests for the PagedModel
.