Unit Test Assert.AreEqual failed

后端 未结 6 1415
别那么骄傲
别那么骄傲 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:37

    If you want to compare two different instances of Supplier, and want them to be considered equal when certain properties have the same value, you have to override the Equals method on Supplier and compare those properties in the method.

    You can read more about the Equals method here: http://msdn.microsoft.com/en-us/library/bsc2ak47.aspx

    Example implementation:

    public override bool Equals(object obj)
    {
        if (obj is Supplier)
        {
            Supplier other = (Supplier) obj;
            return Equals(other.SupplierID, this.SupplierID) && Equals(other.SupplierName, this.SupplierName);
        }
        return false;
    }
    

    Note that you'll also get a compiler warning that you have to implement GetHashCode as well, that could be as simple as this:

    public override int GetHashCode()
    {
        return SupplierID;
    }
    

提交回复
热议问题