Unit Test Error Assert.AreEqual

I have a unit test for a method that gets an object from a collection. This will not work, and I don’t understand why, so I created a very simple test below to create 2 provider objects and check that they are equal in order to see if I can detect a problem in my test on my code. But this test fails again. Can anyone see or explain why?

[TestMethod()] public void GetSupplierTest2() { Supplier expected = new Supplier(); expected.SupplierID = 32532; expected.SupplierName = "Test 1" Supplier actual = new Supplier(); actual.SupplierID = 32532; actual.SupplierName = "Test 1" Assert.AreEqual(expected, actual); } 

But if I test the individual properties of objects, the test passes ...

  [TestMethod()] public void GetSupplierTest2() { Supplier expected = new Supplier(); expected.SupplierID = 32532; expected.SupplierName = "Test 1" Supplier actual = new Supplier(); actual.SupplierID = 32532; actual.SupplierName = "Test 1" Assert.AreEqual(expected.SupplierID , actual.SupplierID ); Assert.AreEqual(expected.SupplierName , actual.SupplierName ); } 
+10
source share
6 answers

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

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

Implementation Example:

 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 will also receive a compiler warning that you need to implement GetHashCode as well, it can be that simple:

 public override int GetHashCode() { return SupplierID; } 
+4
source

Like any other answer, the problem is that you are trying to compare Supplier instances [probably] without overriding the Equals method. But I don’t think you should redefine Equals for testing purposes , as this may affect the production code, or you may need different Equals logic in the production code.

Instead, you must either state each member one by one, as you do in the first example (unless you have a large number of places where you want to compare the entire object), or encapsulate this comparison logic in a certain 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); 
+22
source

The standard implementation of Object.Equals for reference types (such as classes) is "Reference Equality": both objects are actually the same instance. It does not compare field values.

Or (as others have shown) override Equals to give "Value Equality". In this case, you must also override GetHashCode (so the containers work) and you must override operator == .

Alternatively, recognize that most objects must have reference equality (two providers with the same name are not always the same organization) and actually use the properties directly.

+3
source

You are comparing 2 different instances of the Provider type, why Assert fails.

If you want the Providers to be equal, they say (by their Id ), you can override the Equals method, here is a very simple 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; } } 
+2
source

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

When testing parent objects, you compare only two structures of containers of the "Provider" type, and even if they can contain equal property values, they are not equal: since you create two separate objects, they are not in the same address in memory.

+1
source
 //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 regard to disputes about accessing private fields in unit tests, I believe that it should never be used unless it is absolutely necessary (for example, time and expense management issues). * /

0
source

All Articles