RSpec is doing a Samoa on me

I

Iain Barnett

Hi,

RSpec is taking a day off the date in my tests:

RSpec:

let:)vtime_start){ Time.now - 10 }

(logged) =3D> 2011-05-10 15:57:39 +0100

irb:

Time.now - 10
=3D> 2011-05-11 15:57:50 0100

system commandline:

$ date =
=20
=3D> Wed 11 May 2011 16:01:00 BST

irb again:

(Time.parse `date`.chomp) - 10
=3D> 2011-05-11 16:02:06 0100

I'm running ruby 1.9.2p0 (2010-08-18) [x86_64-darwin10.4.0], OSX 10.6.6, =
RSpec 2.5.2

Is there something about the way Ruby deals with time that I should look =
at, or have I done something stupid, or should I move this over to the =
RSpec list?

Any help is much appreciated.

Regards,
Iain=
 
B

Brian Candler

I suggest you make a completely standalone rspec test until you find
what the problem is. It Works For Me [TM].

$ cat ert.rb
require 'rspec'

describe "time" do
let:)vtime_start){ Time.now - 10 }
it "should work" do
puts Time.now
puts vtime_start
end
end
$ ruby -rubygems ert.rb
Wed May 11 16:31:42 +0100 2011
Wed May 11 16:31:32 +0100 2011
 
D

David Jacobs

[Note: parts of this message were removed to make it a legal post.]

Could it be a problem with the logger you're using? Try doing a puts
instead.

David
 
I

Iain Barnett

Could it be a problem with the logger you're using? Try doing a puts
instead.

Hi,

puts gave me this:

vtime_start: 2011-05-10 17:11:54 +0100


I only started logging it because the test was failing, and the code =
it's testing is simple, I can just run it in irb and it works, hence the =
logging.

It's very frustrating! :(

Regards,
Iain=
 
C

Chris Hulan

Could a change in another preceding test be leaking into this one?
I get that sometimes in my Python unit tests when I'm not careful
 
I

Iain Barnett

Could a change in another preceding test be leaking into this one?
I get that sometimes in my Python unit tests when I'm not careful


I suggest you make a completely standalone rspec test until you find=20=
what the problem is. It Works For Me [TM].


It shouldn't (according to my wishes:) have been leaking stuff as each =
test uses a fresh instance to test with and then sets the instance var =
using `instance_variable_set` from the local vstart_time, but I =
separated out the test, used a `lets:)blah){ Blah.new }` instead of a =
`before:)each){ @blah =3D Blah.new }` and that got the time to work =
properly, then put it back in.


Looking at the previous stuff I still can't fathom why Time.now should =
be affected the way it was. Surely Time.now is static and using a `let` =
for vstart_time should insulate against leaks?


As an aside, TimeCop is a great way to "freeze time" during unit tests = that
rely on Time.now:
=20
https://github.com/jtrupiano/timecop

I started using this and it's made the tests a lot better.


Many thanks for all the help and suggestions, it's made the swear jar a =
lot poorer :)

Regards,
Iain
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top