[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Time Test::Unit Question

fREW

7/3/2007 8:58:00 PM

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?

--
Posted via http://www.ruby-....

8 Answers

Dan Zwell

7/4/2007 4:17:00 AM

0

Frew Schmidt 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

fREW

7/4/2007 11:54:00 PM

0

> 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

--
Posted via http://www.ruby-....

John Wilger

7/5/2007 5:27:00 PM

0

On Jul 4, 4:54 pm, Frew Schmidt <fri...@gmail.com> wrote:
> 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.

I've been using the foloowing code for a while to do exactly that:

## BEGIN time_warp.rb ##
class Time #:nodoc:
class <<self
attr_accessor :warp
alias_method :real_now, :now
def now
warp
end
alias_method :new, :now
end
end
Time.warp = Time.real_now

def pretend_now_is(time)
begin
Time.warp = time
yield
ensure
Time.warp = Time.real_now
end
end
## END time_warp.rb ##

I'm not the original author. I grabbed the code of one of the numerous
code pasting sites quite a while ago, and I no longer remember exactly
where it came from.

At any rate, you can use that in your tests like so:

## Begin test_relative_time.rb ##
require 'time_warp'
require 'test/unit'

class TestRelativeTime < Test::Unit::TestCase
DAY = 86400
def test_should_not_really_be_within_1_day_of_2005_07_04
assert( ( Time.now - Time.mktime( 2005, 7, 4 ) ).abs > DAY )
end

def test_should_pretend_to_be_within_1_day_of_2005_07_04
pretend_now_is( Time.mktime( 2005, 7, 5 ) ) do
assert( ( Time.now - Time.mktime( 2005, 7, 4 ) ).abs <= DAY )
end
end
end
## END test_relative_time.rb ##

Hope that helps.

--
Regards,

John Wilger
http://john...

Eric Hodel

7/6/2007 8:26:00 PM

0

On Jul 4, 2007, at 16:54, Frew Schmidt wrote:

>> 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.

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.

--
Poor workers blame their tools. Good workers build better tools. The
best
workers get their tools to do the work for them. -- Syndicate Wars



James Mead

7/7/2007 10:24:00 AM

0

You could try using Mocha (http://mocha.rub...)...

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

--
James.
http://blog.floe...

fREW

7/7/2007 3:27:00 PM

0

James Mead wrote:
> You could try using Mocha (http://mocha.rub...)...
>
> 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.

--
Posted via http://www.ruby-....

James Mead

7/7/2007 4:37:00 PM

0

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.
http://blog.floe...

aurelianito

7/7/2007 4:57:00 PM

0

> James Mead wrote:
> > You could try using Mocha (http://mocha.rub...)...
> >
> > 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.