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.

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

Can you find a way to subtract the test field’s time from “now”, and
allow a delta of (say) 10 seconds?

–Michael

I’d love to use assert_in_delta but since I’m using assert_select I
can’t figure out how to something similar.

Michael G. wrote:

Can you find a way to subtract the test field’s time from “now”, and
allow a delta of (say) 10 seconds?

–Michael

Clem R. wrote:

I’d love to use assert_in_delta but since I’m using assert_select I
can’t figure out how to something similar.

assert_select yields the found elements (HTML::Node), so you can try
something like:

assert_select(“pubDate”) do |el|
assert_in_delta el.to_s, …
end

On Nov 5, 2007 4:17 PM, Clem R. [email protected]
wrote:

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

I think the most promising approach is to “freeze” time for the
duration of the test. Various approaches have been discussed. I think
something like this would work well:

http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/195144

Here’s a similar approach of my own design:

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

Bob,

 This was perfect!   Thanks so much for this approach.

This has been driving me nuts for a while and this approach solves all
my time problems.

Cheers.

Bob S. wrote:

On Nov 5, 2007 4:17 PM, Clem R. [email protected]
wrote:

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

I think the most promising approach is to “freeze” time for the
duration of the test. Various approaches have been discussed. I think
something like this would work well:

http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/195144

Here’s a similar approach of my own design:

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