Valimai
asked on
Unit test failing because of millisecond compare on (DateTime?) nullable type (.NET 2.0)
Hi,
I have an assert
Assert.AreEqual(savedUser. DateCreate d, loadUser.DateCreated, "DateCreated");
The problem is that it fails because loading up from SQL changes the value of the milliseconds (i know why).
My question is, how do i compare dates?
Note: User.DateCreated is type (DateTime?), nullable
ORIGINAL ASSERT
Assert.AreEqual(savedUser. DateCreate d, loadUser.DateCreated, "DateCreated");
ERROR
Test_UserData.Load : DateCreated
Expected: 2007-10-11 08:51:01.638
But was: 2007-10-11 08:51:01.640
NEXT ATTEMPT
Assert.AreEqual(savedUser. DateCreate d.Value.To String("yy yymmnn H:nn:ss"), loadUser.DateCreated.Value .ToString( "yyyymmnn H:nn:ss"), "DateCreated");
ERROR
Test_UserData.Load : System.InvalidOperationExc eption : Nullable object must have a value.
That is because DateCreated is null.
How do i compare without the millisecond component in an efficient manner?
thanks
I have an assert
Assert.AreEqual(savedUser.
The problem is that it fails because loading up from SQL changes the value of the milliseconds (i know why).
My question is, how do i compare dates?
Note: User.DateCreated is type (DateTime?), nullable
ORIGINAL ASSERT
Assert.AreEqual(savedUser.
ERROR
Test_UserData.Load : DateCreated
Expected: 2007-10-11 08:51:01.638
But was: 2007-10-11 08:51:01.640
NEXT ATTEMPT
Assert.AreEqual(savedUser.
ERROR
Test_UserData.Load : System.InvalidOperationExc
That is because DateCreated is null.
How do i compare without the millisecond component in an efficient manner?
thanks
ASKER
ok, i ended up doing this for each of my dates. 4 in this object.
CompareNullableDate(savedU ser.DateCr eated, loadUser.DateCreated, "DateCreated");
private static void CompareNullableDate(DateTi me? expectedDate, DateTime? compareDate, string fieldName)
{
if (expectedDate == null)
{
Assert.IsNull(expectedDate );
}
else
{
Assert.AreEqual(expectedDa te.Value.T oString("y yyymmnn H:nn:ss"), compareDate.Value.ToString ("yyyymmnn H:nn:ss"), fieldName);
}
}
CompareNullableDate(savedU
private static void CompareNullableDate(DateTi
{
if (expectedDate == null)
{
Assert.IsNull(expectedDate
}
else
{
Assert.AreEqual(expectedDa
}
}
ASKER
changed
Assert.IsNull(expectedDate , fieldName);
Assert.IsNull(expectedDate
ASKER
whoops, i meant
Assert.IsNull(compareDate, fieldName);
Assert.IsNull(compareDate,
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Bob