RailsCasts Pro episodes are now free!

Learn more or hide this

Leif Bladt's Profile

GitHub User: leifbladt

Site: http://twitter.com/leifbladt

Comments by Leif Bladt

Avatar

You're right. The Timecop version is more deterministic and that is what our tests should be.

But there maybe situations where you can't use Timecop and you don't need to test on milliseconds (with an increased delta to get a more robust test), then it might be a good solution too.

Avatar

Testing with times involved is mostly no fun, particularly because I never heard of Timecop before :) But I found a way to manage it with RSpec matchers:

ruby
it "saves the time the password reset was sent" do
  user.send_password_reset
  user.reload.password_reset_sent_at.should be_within(1.second).of(Time.zone.now)
end