# Problems testing w/ seconds at runtime

**URL:** <https://www.ruby-forum.com/t/problems-testing-w-seconds-at-runtime/119300>\
**Category:** Rails\
**Created:** [November 5, 2007, 10:17pm UTC](https://www.ruby-forum.com/t/problems-testing-w-seconds-at-runtime/119300 "2007-11-05T22:17:09Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![clemrock](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c57346/32.png) [@clemrock](https://www.ruby-forum.com/u/clemrock)\
**Post date:** [November 5, 2007, 10:17pm UTC](https://www.ruby-forum.com/t/problems-testing-w-seconds-at-runtime/119300/1 "2007-11-05T22:17:09Z")

</div>

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.

```auto
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

---

<div class="post-metadata">

**Author:** ![clemrock](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c57346/32.png) [@clemrock](https://www.ruby-forum.com/u/clemrock)\
**Post date:** [November 9, 2007, 1:27am UTC](https://www.ruby-forum.com/t/problems-testing-w-seconds-at-runtime/119300/2 "2007-11-09T01:27:54Z")

</div>

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

–Michael

---

<div class="post-metadata">

**Author:** ![clemrock](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c57346/32.png) [@clemrock](https://www.ruby-forum.com/u/clemrock)\
**Post date:** [November 14, 2007, 4:41pm UTC](https://www.ruby-forum.com/t/problems-testing-w-seconds-at-runtime/119300/3 "2007-11-14T16:41:33Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![clemrock](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c57346/32.png) [@clemrock](https://www.ruby-forum.com/u/clemrock)\
**Post date:** [November 15, 2007, 7:58pm UTC](https://www.ruby-forum.com/t/problems-testing-w-seconds-at-runtime/119300/4 "2007-11-15T19:58:10Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![clemrock](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c57346/32.png) [@clemrock](https://www.ruby-forum.com/u/clemrock)\
**Post date:** [November 15, 2007, 10:38pm UTC](https://www.ruby-forum.com/t/problems-testing-w-seconds-at-runtime/119300/5 "2007-11-15T22:38:36Z")

</div>

On Nov 5, 2007 4:17 PM, Clem R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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](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](http://snippets.dzone.com/posts/show/4776)

---

<div class="post-metadata">

**Author:** ![clemrock](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c57346/32.png) [@clemrock](https://www.ruby-forum.com/u/clemrock)\
**Post date:** [November 19, 2007, 6:21am UTC](https://www.ruby-forum.com/t/problems-testing-w-seconds-at-runtime/119300/6 "2007-11-19T06:21:12Z")

</div>

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. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> 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](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](http://snippets.dzone.com/posts/show/4776)
