shouldly / shouldly

Should testing for .NET—the way assertions should be!

Home Page:https://docs.shouldly.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ShouldBe does not use the correct comparer for IEnumerable<IEquatable>

rick-palmsens opened this issue · comments

For the following code, comparing the individual instances with ShouldBe succeeds, but comparing the lists fails. It seems to call the object version of Equals when comparing the lists, which has not been overridden. The individual comparisons however do call the Foo version of Equals.

using System;
using System.Collections.Generic;
using Shouldly;

namespace ShouldlyEquatable
{
    public class Foo : IEquatable<Foo>
    {
        public int Bar { get; set; }

        public bool Equals(Foo other)
        {
            if (ReferenceEquals(null, other)) return false;
            return Bar == other.Bar;
        }
    }

    public class FooTests
    {
        public void Equatable()
        {
            var expected = new List<Foo>
            {
                new Foo { Bar = 1 },
                new Foo { Bar = 2 },
                new Foo{ Bar = 3 }
            };

            var actual = new List<Foo>
            {
                new Foo { Bar = 1 },
                new Foo { Bar = 2 },
                new Foo{ Bar = 3 }
            };

            for (int i = 0; i < expected.Count; ++i)
            {
                actual[i].ShouldBe(expected[i]); // Succeeds
            }

            actual.ShouldBe(expected); // Fails
        }
    }
}