Time Test::Unit Question

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?

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

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

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

Time.stubs(:now).returns(the_time_you_want)

James M. wrote:

You could try using Mocha (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.

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.

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…?

James M. wrote:

You could try using Mocha (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.