Mike
Mike

Reputation: 9842

Timecop and Ruby 2.0.0

Can anyone suggest why this spec fails using Timecop 0.6.1 and Ruby 2.0.0?

(It passes using Timecop 0.4.5 and Ruby 1.9.3)

require 'timecop'
require 'spec_helper'

describe Class do
  it "freezes time" do
    Timecop.freeze Date.new(2012,7,1) do
      expect(Date.today).to eq(Date.new 2012,7,1)
    end
  end
end

Which results in:

$ rspec spec/models/time_cop_spec.rb 

Failures:

  1) Class freezes time
     Failure/Error: expect(Date.today).to eq(Date.new 2012,7,1)

       expected: Sun, 01 Jul 2012
            got: Thu, 02 May 2013

       (compared using ==)

       Diff:
       @@ -1,2 +1,2 @@
       -Sun, 01 Jul 2012
       +Thu, 02 May 2013

Upvotes: 0

Views: 1740

Answers (3)

Mike
Mike

Reputation: 9842

There is a very clear and consise answer to this (and a pull request to fix it) on Timecop's GitHub Repo.

Short answer: a change in Bundler version, not a change in Ruby or Timecop version, caused the issue.

Short solution: Add require 'date' to the very top (before require 'timecop').

Thanks to @micahchalmer :)

Upvotes: 1

David Miani
David Miani

Reputation: 14678

Are you sure you aren't suffering from another issue? I ran you code using ruby 2.0.0-p0, with timecop 0.6.1, and it functioned fine. Ie running the code:

require 'timecop'
puts "ruby version: #{RUBY_VERSION}"
puts "timecop version: #{Timecop::VERSION}"

describe Class do
  it "freezes time" do
    Timecop.freeze Date.new(2012,7,1) do
      expect(Date.today).to eq(Date.new 2012,7,1)
    end
  end
end

Results in:

$ rspec test.rb
ruby version: 2.0.0
timecop version: 0.6.1
.

Finished in 0.00082 seconds
1 example, 0 failures

Upvotes: 0

Mike
Mike

Reputation: 9842

The following passes so it appears Date.today support has been dropped in favour of Time.now.to_date.

require 'timecop'
require 'spec_helper'

describe Class do
  it "freezes time" do
    Timecop.freeze Date.new(2012,7,1) do
      expect(Time.now.to_date).to eq(Date.new 2012,7,1)
    end
  end
end

Upvotes: 0

Related Questions