PDA

View Full Version : Problems testing w/ seconds at runtime


clem_c_rock
November 5th, 2007, 04:18 PM
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.


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

clem_c_rock
November 19th, 2007, 12:13 PM
Found the solution and it works great!

Freezing time for test:

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