?
Solved

Unit test failing because of millisecond compare on (DateTime?) nullable type (.NET 2.0)

Posted on 2007-10-10
5
Medium Priority
?
330 Views
Last Modified: 2008-01-09
Hi,

I have an assert
Assert.AreEqual(savedUser.DateCreated, 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.DateCreated, 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.DateCreated.Value.ToString("yyyymmnn H:nn:ss"), loadUser.DateCreated.Value.ToString("yyyymmnn H:nn:ss"), "DateCreated");
ERROR
Test_UserData.Load : System.InvalidOperationException : 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
0
Comment
Question by:Valimai
  • 3
5 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 20053760
If you are working with a Nullable type, then I would check that DateCreated has a value before asserting that the values are equal.

Bob
0
 
LVL 1

Author Comment

by:Valimai
ID: 20053937
ok, i ended up doing this for each of my dates. 4 in this object.

CompareNullableDate(savedUser.DateCreated, loadUser.DateCreated, "DateCreated");

private static void CompareNullableDate(DateTime? expectedDate, DateTime? compareDate, string fieldName)
{
      if (expectedDate == null)
      {
            Assert.IsNull(expectedDate);
      }
      else
      {
            Assert.AreEqual(expectedDate.Value.ToString("yyyymmnn H:nn:ss"), compareDate.Value.ToString("yyyymmnn H:nn:ss"), fieldName);
      }
}
0
 
LVL 1

Author Comment

by:Valimai
ID: 20054089
changed
Assert.IsNull(expectedDate, fieldName);
0
 
LVL 1

Author Comment

by:Valimai
ID: 20054091
whoops, i meant
Assert.IsNull(compareDate, fieldName);
0
 
LVL 25

Accepted Solution

by:
dstanley9 earned 1000 total points
ID: 20056680
In addition to checking for null, to check for dates that are very close you could calculate the distance between them as a TimeSpan and make sure it's less than a certain amount:

// ensure the times are less than one second apart
TimeSpan diff = dt1 - dt2;
int seconds = diff.Seconds;
Assert.AreEqual(seconds, 0,"The two times are more than one second apart");
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question