neo
neo

Reputation: 4116

Invalid Time.now on RSpec

How do I set the correct Time on RSpec.

[2] pry(#<RSpec::ExampleGroups::Device::Creation::Security>)> Time.now
=> Fri, 01 Jan 2016 10:00:00 CET +01:00

On rails console I have the correct time:

[1] pry(main)> Time.now
=> 2015-12-11 12:41:36 -0500

I don't want to stub it, because I'm not passing the time to my class. Have the following method in the class I'm testing:

  def within_expiration?
    Time.now - user.sms_sent_date < time_limit_in_seconds
  end

This is not behaving as expected because Time.now is Jan 1, 2016

user.sms_sent_date has right date.

Upvotes: 1

Views: 164

Answers (1)

Simone Carletti
Simone Carletti

Reputation: 176472

Make sure you don't have any library that is currently altering your Time in the spec.

For example, one of the most commonly used is Timecop. Make sure you don't setup it globally, instead use the blocks or before/after to properly mock the date and teardown the mock when no longer required.

Upvotes: 2

Related Questions