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

Posted on 2007-10-10
Last Modified: 2008-01-09

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

Assert.AreEqual(savedUser.DateCreated, loadUser.DateCreated, "DateCreated");
Test_UserData.Load :   DateCreated
  Expected: 2007-10-11 08:51:01.638
  But was:  2007-10-11 08:51:01.640

Assert.AreEqual(savedUser.DateCreated.Value.ToString("yyyymmnn H:nn:ss"), loadUser.DateCreated.Value.ToString("yyyymmnn H:nn:ss"), "DateCreated");
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?

Question by:Valimai
    LVL 96

    Expert Comment

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

    LVL 1

    Author Comment

    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.AreEqual(expectedDate.Value.ToString("yyyymmnn H:nn:ss"), compareDate.Value.ToString("yyyymmnn H:nn:ss"), fieldName);
    LVL 1

    Author Comment

    Assert.IsNull(expectedDate, fieldName);
    LVL 1

    Author Comment

    whoops, i meant
    Assert.IsNull(compareDate, fieldName);
    LVL 25

    Accepted Solution

    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");

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Join & Write a Comment

    Suggested Solutions

    Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
    Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    754 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

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now