Unit Test Assert.AreEqual failed

后端 未结 6 1404
别那么骄傲
别那么骄傲 2021-01-17 16:12

I have a unit test for a method which gets an object from a collection. This keeps failing and I cannot see why, so I have created a very simple test below to create 2 suppl

6条回答
  •  难免孤独
    2021-01-17 16:53

    You compare 2 different instances of the Supplier type, that's why Assert fail.

    If you want to Supplier are equals say (by their Id) you can override Equals method, here very over simpled example, :D.

    public class Supplier
    {
        private int id;
        private string name;
    
        public int Id
        {
            get { return id; }
        }
    
        public string Name
        {
            get { return name; }
        }
    
        public bool Equals(Supplier other)
        {
            if(other == null) return false;
            return other.id == id;
        }
    
        public override bool Equals(object obj)
        {
            if(obj == null) return false;
            if (obj.GetType() != typeof (Supplier)) return false;
            return Equals((Supplier) obj);
        }
    
        public override int GetHashCode()
        {
            return id;
        } 
    }
    

提交回复
热议问题