Unit Test Assert.AreEqual failed

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

    The default implementation of Object.Equals for reference types (ie. classes) is "Reference Equality": are the two objects actually the same instance. It doesn't compare the values of fields.

    Either (as others have shown) override Equals to give "Value Equality". In this caseyou must also override GetHashCode (so containers work), and should override operator ==.

    Alternatively accept that most entities should have reference equality (two suppliers with the same name are not always the same organisation) and actually use the properties directly.

    0 讨论(0)
  • 2021-01-17 16:30
    //Given the following structure and an instance value, targetObj...
    
    class BaseType
    {
       private FeatureType Feature_1;
    }
    
    class TargetType : BaseType 
    {
      ...
    }
    
    TargetType targetObj = new TargetType();
    
    //...a private feature in a specific base class can be accessed as follows
    
    PrivateType typeCast = new PrivateType(typeof( BaseType ));
    
    PrivateObject privateObj = new PrivateObject(targetObj, typeCast);
    
    //...and values can be retrieved and set as follows....
    
    privateObj.SetField("Feature_1", (FeatureType) newValue );
    
    FeatureType result = (FeatureType) privateObj.GetField("Feature_1");
    

    /* With respect to the controversy around accessing private fields in unit tests, I believe it should never be used unless absolutely neccessary (i.e. time and expense management issues). */

    0 讨论(0)
  • 2021-01-17 16:33

    As every other answer says the issue is that you're trying to compare instances of Supplier [probably] without overriding Equals method. But I do not think you should override Equals for test purposes since it may affect production code or you may need another Equals logic in production code.

    Instead you should either assert each member one by one as you do it in first sample (if you do not have a lot of places where you want to compare entire object) or encapsulate this comparison logic in some class and use this class:

    static class SupplierAllFieldsComparer
    {
        public static void AssertAreEqual(Supplier expected, Supplier actual)
        {
            Assert.AreEqual(expected.SupplierID , actual.SupplierID );
            Assert.AreEqual(expected.SupplierName , actual.SupplierName );            
        }
    }
    

    // Test code:

    SupplierAllFieldsComparer.AssertAreEqual(expected, actual);
    
    0 讨论(0)
  • 2021-01-17 16:36

    When testing the individual properties, you compare the string/integer values. They are equal, and so the tests pass.

    When testing the parent objects, you compare only the two container structures of type Supplier - and even though those may hold equal property values, they are not equal: Since you are instantiating two separate objects, they do not reside at the same address in memory.

    0 讨论(0)
  • 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;
    }
    
    0 讨论(0)
  • 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;
        } 
    }
    
    0 讨论(0)
提交回复
热议问题