SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Zealot
    Join Date
    Jul 2004
    Location
    NC
    Posts
    194
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Problems testing w/ seconds at runtime

    Hello,

    I constantly run into this problem when trying to test assertions against time w/ seconds. Since there can be a tiny lapse time between when a test is fired off and the value is tested your test can be off by a second, therefor causing it to fail.

    For instance. I have an assert select test that will randomly fail because the dynamic time being tested against will have lapsed a second.

    Code:
    assert_select "pubDate", 6.days.from_now.gmtime.strftime("%a, %d %b %Y %I:%M:%S GMT")
    Now, about 50% of the time, this test will fail and give you a result like such:

    <"Sun, 11 Nov 2007 21:15:48 GMT"> expected but was
    <"Sun, 11 Nov 2007 21:15:47 GMT">.

    Any ideas on how I can make this test less brittle?

    Thanks,
    Eric

  2. #2
    SitePoint Enthusiast
    Join Date
    May 2001
    Location
    Kaysville, UT
    Posts
    68
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I haven't run into this specific issue with Ruby yet, but I have run into similar time testing issues in other languages. Can you use the same date instance in both spots? Ie, instead of creating a new date and adding 6 days to it, use the original date and add 6 days to it? What does the rest of your test look like?
    Spencer Uresk
    Rails App Hosting
    Cheap Hosting - Directory of hosting plans under $10/month!
    My blog: Tech and Java Articles

  3. #3
    SitePoint Zealot
    Join Date
    Jul 2004
    Location
    NC
    Posts
    194
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Found the solution and it works great!

    Freezing time for test:

    http://snippets.dzone.com/posts/show/1738


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •