Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Time.freeze doesn't seem to freeze time #146

Closed
davidpdrsn opened this issue Mar 22, 2015 · 4 comments
Closed

Time.freeze doesn't seem to freeze time #146

davidpdrsn opened this issue Mar 22, 2015 · 4 comments

Comments

@davidpdrsn
Copy link

I'm having some issues with Timecop.freeze. It works fine on OS X, but the same test fails on Arch Linux. Not sure if this is because of Timecop or rspec.

I've an example app with a failing test

https://github.com/davidpdrsn/timecop-test-failure

User spec

require "rails_helper"

describe User do
  describe "seen!" do
    it "sets last_seen" do
      Timecop.freeze(Time.now) do
        user = User.create name: "Bob"

        user.seen!

        expect(User.find(user.id).last_seen).to eq Time.now
      end
    end
  end
end

User model

class User < ActiveRecord::Base
  def seen!
    update last_seen: Time.now
  end
end

Test output

F

Failures:

  1) User seen! sets last_seen
     Failure/Error: expect(User.find(user.id).last_seen).to eq Time.now

       expected: 2015-03-22 20:49:15.899984404 +0100
            got: 2015-03-22 19:49:15.899984000 +0000

       (compared using ==)

       Diff:
       @@ -1,2 +1,2 @@
       -2015-03-22 20:49:15 +0100
       +Sun, 22 Mar 2015 19:49:15 UTC +00:00

     # ./spec/models/user_spec.rb:11:in `block (4 levels) in <top (required)>'
     # ./spec/models/user_spec.rb:6:in `block (3 levels) in <top (required)>'
     # -e:1:in `<main>'

Finished in 0.01691 seconds (files took 0.22619 seconds to load)
1 example, 1 failure

Failed examples:

rspec ./spec/models/user_spec.rb:5 # User seen! sets last_seen

Might be related to #97, but as I said it works on OS X, but not Arch Linux.

@sindrenm
Copy link

sindrenm commented May 9, 2015

I am experiencing the same on Arch, although my timezone doesn't change like yours do. On OS X Yosemite everything is working fine.

@sindrenm
Copy link

Here is the error I am getting:

Failure/Error: expect(user.reload.last_heist_at).to eq Time.zone.now

  expected: 2015-05-09 22:52:56.280502338 +0200
       got: 2015-05-09 22:52:56.280502000 +0200

  (compared using ==)

  Diff:

@sindrenm
Copy link

Hang on! This looks like it might be a difference in the database setup or something. Notice how the actual timestamps are less accurate than what is expected? The same happens in your example, @davidpdrsn. The timestamps are probably persisted with less precision in our Arch installations or something.

@sindrenm
Copy link

Yeah, that's it. Looking at the issue you referenced earlier, @davidpdrsn, this was mentioned in #97 (comment).

mcordell added a commit to mcordell/grape_token_auth that referenced this issue Sep 7, 2015
The db configuration can effect the percision of timestamps. See this:
travisjeffery/timecop#146 . Works locally of
course because of local db setup, CI it failed, so this just truncates
at the millisecond.
vincemtnz added a commit to SumOfUs/Champaign that referenced this issue Apr 26, 2018
It seems like db config can timestamp precision, so I'm amending
the test to expect `consented_at` to be withih a second. It could
probably be set to be within a milisecond (since the variance I've
observed is much smaller) but I'm keeping it simple and
permissive.

Variance observed in CI:
-  expected: 2018-04-26 17:37:07.235862273 +0000
-       got: 2018-04-26 17:37:07.235862000 +0000

See travisjeffery/timecop#146
vincemtnz added a commit to SumOfUs/Champaign that referenced this issue May 1, 2018
It seems like db config can timestamp precision, so I'm amending
the test to expect `consented_at` to be withih a second. It could
probably be set to be within a milisecond (since the variance I've
observed is much smaller) but I'm keeping it simple and
permissive.

Variance observed in CI:
-  expected: 2018-04-26 17:37:07.235862273 +0000
-       got: 2018-04-26 17:37:07.235862000 +0000

See travisjeffery/timecop#146
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants