Petr Gazarov
Petr Gazarov

Reputation: 3821

Time.now returns wrong result in test environment

I'm running an RSpec test suite with capybara in my Rails app. When I put binding.pry inside of a test, and try Time.current, I get => Tue, 15 Dec 2015 00:06:30 UTC +00:00, which is three months ago. As far as my basic knowledge of geography goes, there isn't a time zone that has a 3 months difference with EST. So what am I missing here? :-)

Running same query in development environment (i.e. through rails console) returns correct result.

Upvotes: 0

Views: 662

Answers (1)

Paul Fioravanti
Paul Fioravanti

Reputation: 16793

It sounds like you have specs in your suite that use the Timecop gem. So, have a look for every instance of Timecop.freeze and ensure there is a corresponding Timecop.return statement in order to do proper teardown of the time freeze and prevent the unexpected side effect of it leaking into other specs in your suite. Here is an example from the gem's README:

describe "some set of tests to mock" do
  before do
    Timecop.freeze(Time.local(1990))
  end

  after do
    Timecop.return
  end

  it "should do blah blah blah" do
  end
end

Upvotes: 3

Related Questions