views:

340

answers:

6

my C# unit test has the following statement:

Assert.AreEqual(logoutTime, log.First().Timestamp);

Why it is failed with following information:

Assert.AreEqual failed. Expected:<4/28/2010 2:30:37 PM>. Actual:<4/28/2010 2:30:37 PM>.

Are they not the same?

EDIT

Use this if you only care to second:

Assert.AreEqual(logoutTime.ToString(), log.First().Timestamp.ToString());

+16  A: 

Have you verified that the number of ticks/milliseconds are equal?

If you do DateTime.Now() twice back to back, they will appear to be the same number down to the minute and probably even down to the second, but they will in fact vary by ticks. If you want to check equality only to the minute, compare each DateTime only to that degree. For information on rounding DateTimes, see here

Dinah
+1  A: 

Are you sure that logoutTime and log.First().Timestamp are both typed as DateTime?

If so, they might also have different values for the more specific time infomation (e.g., milliseconds).

manu08
A: 

Try something like Assert.AreEqual(logoutTime.Ticks, log.First().Timestamp.Ticks)

Germ
As Jon Skeet says on jlech's answer: "That will do exactly the same thing, other than showing the different values of ticks in the exception message."
Dinah
Exactly, so it will show you why the two values aren't equal.
Germ
A: 

I suppose Assert.AreEqual<T> uses Object.Equals() to determine equality of the objects but not the values.

Probably this statement is comparing two different objects and therefore is returning false.

Asad Butt
+1  A: 

Assuming that logoutTime and log.First().Timestamp are both of type DateTime, you should try using this instead:

Assert.AreEqual(logoutTime.Ticks, log.First().Timestamp.Ticks);
jlech
That will do exactly the same thing, other than showing the different values of ticks in the exception message.
Jon Skeet
A: 

The Assert fail method is probably calling ToString() on the DateTime which returns a truncated, human-readable form of the date without the milliseconds component. This is why it appears they are equal when, in fact, the DateTime object has a precision of a 100-nanosecond unit (known as a Tick). That means it is highly unlikely two DateTime objects will have the exact same value. To compare you probably want to truncate the value, perhaps by formatting the date to the fidelity you require.

Dan Diplo