Info: using .NET 4.0 and VS 2012
Hi,
I'm about to unit test my own class which has got a member of type TimeZoneInfo. However, when I try to consider this member in my test, it always fails.
The following is a simplified example which verifies that _timeZone has correctly been initialized during instantiation process:
public class MyClass
{
public TimeZoneInfo _timeZone;
public MyClass(string timeZoneId)
{
_timeZone = TimeZoneInfo.FindSystemTimeZoneById(timeZoneId);
}
}
[TestMethod()]
public void MyClassCtorTest()
{
TimeZoneInfo expected = TimeZoneInfo.FindSystemTimeZoneById("W. Europe Standard Time");
TimeZoneInfo actual = new MyClass("W. Europe Standard Time")._timeZone;
Assert.IsTrue(expected.Equals(actual)); //This test passes!
Assert.AreEqual(expected, actual); //This test fails!
}
I found out that Assert.IsTrue(...) passes whereas Assert.AreEqual(...) fails: "Assert.AreEqual failed. Expected:<(UTC+01:00) Amsterdam, Berlin, Bern, Rom, Stockholm, Wien>. Actual:<(UTC+01:00) Amsterdam, Berlin, Bern, Rom, Stockholm, Wien>."
Since "Equals" is overridden in TimeZoneInfo class, I don't get what's going on, here. Could you help me to make pass the second assertion? Thank you very much in advance!
Rob
Your passing test calls
bool Equals(TimeZoneInfo)
Your failing test implicitly calls
bool Equals(object)
In the .NET 4.0 version of TimeZoneInfo
, Equals(object)
has not been overridden; in .NET 4.5 it has.
See more on this question at Stackoverflow