MSTest: CollectionAssert.AreEquivalent failed. The expected collection contains 1 occurrence(s) of

前端 未结 5 586
生来不讨喜
生来不讨喜 2020-12-08 20:23

Question:

Can anyone tell me why my unit test is failing with this error message?

CollectionAssert.AreEquivalent failed. The

相关标签:
5条回答
  • 2020-12-08 20:52

    It works if I add an IEqualityComparer<T> as described on MSDN and if I use Enumerable.SequenceEqual. Note however, that now the order of the elements is relevant.

    In the unit test

    //CollectionAssert.AreEquivalent(list1, list2); // Does not work
    Assert.IsTrue(list1.SequenceEqual(list2, new MyPersonEqualityComparer())); // Works
    

    IEqualityComparer

    public class MyPersonEqualityComparer : IEqualityComparer<MyPerson>
    {
        public bool Equals(MyPerson x, MyPerson y)
        {
            if (object.ReferenceEquals(x, y)) return true;
    
            if (object.ReferenceEquals(x, null) || object.ReferenceEquals(y, null)) return false;
    
            return x.Name == y.Name && x.Age == y.Age;
        }
    
        public int GetHashCode(MyPerson obj)
        {
            if (object.ReferenceEquals(obj, null)) return 0;
    
            int hashCodeName = obj.Name == null ? 0 : obj.Name.GetHashCode();
            int hasCodeAge = obj.Age.GetHashCode();
    
            return hashCodeName ^ hasCodeAge;
        }
    }
    
    0 讨论(0)
  • 2020-12-08 20:59

    You are absolutely right. Unless you provide something like an IEqualityComparer<MyPerson> or implement MyPerson.Equals(), the two MyPerson objects will be compared with object.Equals, just like any other object. Since the objects are different, the Assert will fail.

    0 讨论(0)
  • 2020-12-08 21:06

    I was getting this same error when testing a collection persisted by nHibernate. I was able to get this to work by overriding both the Equals and GetHashCode methods. If I didn't override both I still got the same error you mentioned:

    CollectionAssert.AreEquivalent failed. The expected collection contains 1 occurrence(s) of . 
    The actual collection contains 0 occurrence(s).
    

    I had the following object:

    public class EVProjectLedger
    {
        public virtual long Id { get; protected set; }
        public virtual string ProjId { get; set; }
        public virtual string Ledger { get; set; }
        public virtual AccountRule AccountRule { get; set; }
        public virtual int AccountLength { get; set; }
        public virtual string AccountSubstrMethod { get; set; }
    
        private Iesi.Collections.Generic.ISet<Contract> myContracts = new HashedSet<Contract>();
    
        public virtual Iesi.Collections.Generic.ISet<Contract> Contracts
        {
            get { return myContracts; }
            set { myContracts = value; }
        }
    
        public override bool Equals(object obj)
        {
            EVProjectLedger evProjectLedger = (EVProjectLedger)obj;
            return ProjId == evProjectLedger.ProjId && Ledger == evProjectLedger.Ledger;
        }
        public override int GetHashCode()
        {
            return new { ProjId, Ledger }.GetHashCode();
        }
    }
    

    Which I tested using the following:

    using (ITransaction tx = session.BeginTransaction())
    {
        var evProject = session.Get<EVProject>("C0G");
    
        CollectionAssert.AreEquivalent(TestData._evProjectLedgers.ToList(), evProject.EVProjectLedgers.ToList());
    
        tx.Commit();
    }
    

    I'm using nHibernate which encourages overriding these methods anyways. The one drawback I can see is that my Equals method is based on the business key of the object and therefore tests equality using the business key and no other fields. You could override Equals however you want but beware of equality pollution mentioned in this post:

    CollectionAssert.AreEquivalent failing... can't figure out why

    0 讨论(0)
  • 2020-12-08 21:10

    If you would like to achieve this without having to write an equality comaparer, there is a unit testing library that you can use, called FluentAssertions,

    https://fluentassertions.com/documentation/

    It has many built in equality extension functions including ones for the Collections. You can install it through Nuget and its really easy to use.

    Taking the example in the question above all you have to write in the end is

    list1.Should().BeEquivalentTo(list2);
    

    By default, the order matters in the two collections, however it can be changed as well.

    0 讨论(0)
  • 2020-12-08 21:19

    I wrote this to test collections where the order is not important:

        public static bool AreCollectionsEquivalent<T>(ICollection<T> collectionA, ICollection<T> collectionB, IEqualityComparer<T> comparer)
        {
            if (collectionA.Count != collectionB.Count)
                return false;
    
            foreach (var a in collectionA)
            {
                if (!collectionB.Any(b => comparer.Equals(a, b)))
                    return false;
            }
    
            return true;
        }
    

    Not as elegant as using SequenceEquals, but it works.

    Of course to use it you simply do:

    Assert.IsTrue(AreCollectionsEquivalent<MyType>(collectionA, collectionB, comparer));

    0 讨论(0)
提交回复
热议问题