# Time Test::Unit Question

**URL:** <https://www.ruby-forum.com/t/time-test-unit-question/108475>\
**Category:** Ruby\
**Created:** [July 3, 2007, 10:58pm UTC](https://www.ruby-forum.com/t/time-test-unit-question/108475 "2007-07-03T22:58:21Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![frew](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/c0e974/32.png) [@frew](https://www.ruby-forum.com/u/frew)\
**Post date:** [July 3, 2007, 10:58pm UTC](https://www.ruby-forum.com/t/time-test-unit-question/108475/1 "2007-07-03T22:58:21Z")

</div>

I am trying to do a Test that has to do with the date of something  
relative to the current time. Should I just have a predefined but  
dynamic dataset or what?

Thoughts?

---

<div class="post-metadata">

**Author:** ![frew](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/c0e974/32.png) [@frew](https://www.ruby-forum.com/u/frew)\
**Post date:** [July 4, 2007, 6:18am UTC](https://www.ruby-forum.com/t/time-test-unit-question/108475/2 "2007-07-04T06:18:10Z")

</div>

Frew S. wrote:

> I am trying to do a Test that has to do with the date of something  
> relative to the current time. Should I just have a predefined but  
> dynamic dataset or what?
> 
> Thoughts?

I would use a set of predefined “random” dates for tests. The reasoning  
(why not just use randomly generated dates?) is that if a test fails, it  
should be reproducible. It would be harder to debug a test that  
sometimes fails.

Dan

---

<div class="post-metadata">

**Author:** ![frew](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/c0e974/32.png) [@frew](https://www.ruby-forum.com/u/frew)\
**Post date:** [July 5, 2007, 1:54am UTC](https://www.ruby-forum.com/t/time-test-unit-question/108475/3 "2007-07-05T01:54:06Z")

</div>

> I would use a set of predefined “random” dates for tests. The reasoning  
> (why not just use randomly generated dates?) is that if a test fails, it  
> should be reproducible. It would be harder to debug a test that  
> sometimes fails.
> 
> Dan

Well the problem is that they will all fail eventually. This is the  
deal, we have date X, let’s say that it’s yesterday. My program uses  
some SQL statements to check if date X (which is obviously in a  
database) was within Y amount of days. But date X will no longer be in  
that range after so many days, so I either have to make it dynamically  
generated (no fun and possible error prone) or somehow make the computer  
think that the current date and time is a certain value.

-fREW

---

<div class="post-metadata">

**Author:** ![frew](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/c0e974/32.png) [@frew](https://www.ruby-forum.com/u/frew)\
**Post date:** [July 7, 2007, 12:25pm UTC](https://www.ruby-forum.com/t/time-test-unit-question/108475/4 "2007-07-07T12:25:28Z")

</div>

You could try using Mocha ([http://mocha.rubyforge.org](http://mocha.rubyforge.org))…

Time.stubs(:now).returns(the\_time\_you\_want)

---

<div class="post-metadata">

**Author:** ![frew](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/c0e974/32.png) [@frew](https://www.ruby-forum.com/u/frew)\
**Post date:** [July 7, 2007, 5:27pm UTC](https://www.ruby-forum.com/t/time-test-unit-question/108475/5 "2007-07-07T17:27:06Z")

</div>

James M. wrote:

> You could try using Mocha ([http://mocha.rubyforge.org](http://mocha.rubyforge.org))…
> 
> Time.stubs(:now).returns(the\_time\_you\_want)

I dig that idea, but I get the current time in the actual DB code. I am  
trying to test my DB code just as much as I am testing my Ruby code  
here.

---

<div class="post-metadata">

**Author:** ![frew](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/c0e974/32.png) [@frew](https://www.ruby-forum.com/u/frew)\
**Post date:** [July 6, 2007, 10:27pm UTC](https://www.ruby-forum.com/t/time-test-unit-question/108475/6 "2007-07-06T22:27:37Z")

</div>

On Jul 4, 2007, at 16:54, Frew S. wrote:

> deal, we have date X, let’s say that it’s yesterday. My program uses  
> some SQL statements to check if date X (which is obviously in a  
> database) was within Y amount of days. But date X will no longer  
> be in  
> that range after so many days, so I either have to make it dynamically  
> generated (no fun and possible error prone) or somehow make the  
> computer  
> think that the current date and time is a certain value.

You should test just the SQL statement, not its results. Let  
somebody else worry about making sure the DB returns the right stuff.

This may mean you need to stub the DB interface to record your  
queries and return your bogus results, but that should only take a  
handful of extra lines.

---

<div class="post-metadata">

**Author:** ![frew](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/c0e974/32.png) [@frew](https://www.ruby-forum.com/u/frew)\
**Post date:** [July 7, 2007, 6:37pm UTC](https://www.ruby-forum.com/t/time-test-unit-question/108475/7 "2007-07-07T18:37:54Z")

</div>

Do you need to get the current time in SQL? Can you not move the logic  
into Ruby where it is easier to test? Of course sometimes it needs to  
be in SQL for performance reasons, but often this is just premature  
optimization.

Perhaps you could share the actual code and SQL with us…?

---

<div class="post-metadata">

**Author:** ![frew](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/c0e974/32.png) [@frew](https://www.ruby-forum.com/u/frew)\
**Post date:** [July 7, 2007, 6:57pm UTC](https://www.ruby-forum.com/t/time-test-unit-question/108475/8 "2007-07-07T18:57:55Z")

</div>

> James M. wrote:
> 
> > You could try using Mocha ([http://mocha.rubyforge.org](http://mocha.rubyforge.org))…
> > 
> > Time.stubs(:now).returns(the\_time\_you\_want)
> 
> I dig that idea, but I get the current time in the actual DB code. I am  
> trying to test my DB code just as much as I am testing my Ruby code  
> here.

Why don’t you calculate the dates in the tests using Time.now as a  
base? Like: yesterday = Time.now - /_1 day_/ (not actual ruby code).  
This should make your tests “time independant” while leaving the data  
base in its actual state and it only adds a little complexity.
