0

I've TimeCop installed and using the travel: option with my tests but my tests seem to be failing when I know they shouldn't be. I thought it was my code but it seems that I'm getting fractions of a second added somewhere which is causing dates that should be equal to not be.

Given the following rspec test:

it 'Testing TimeCop', travel: Time.new(2021, 10, 5, 9, 0, 0, '-07:00') do
  puts "Time.now:                                              #{Time.now}"
  puts "Time.new(2021, 10, 5, 9, 0, 0, '-07:00')}:             #{Time.new(2021, 10, 5, 9, 0, 0, '-07:00')}"
  puts "Time.now == Time.new(2021, 10, 5, 9, 0, 0, '-07:00')}: #{Time.now == Time.new(2021, 10, 5, 9, 0, 0, '-07:00')}"
  puts "Time.now -  Time.new(2021, 10, 5, 9, 0, 0, '-07:00')}: #{Time.now - Time.new(2021, 10, 5, 9, 0, 0, '-07:00')}"
end

I'm getting the following output:

Time.now:                                              2021-10-05 09:00:00 -0700
Time.new(2021, 10, 5, 9, 0, 0, '-07:00')}:             2021-10-05 09:00:00 -0700
Time.now == Time.new(2021, 10, 5, 9, 0, 0, '-07:00')}: false
Time.now -  Time.new(2021, 10, 5, 9, 0, 0, '-07:00')}: 0.0004161418930646181

As you can see these times are not equal and the reason being is there seems to be a 4/10000th of a second discrepancy between the two. I don't know whats going on here. Is there something I'm doing wrong with TimeCop or is this a bug?

aarona
  • 35,986
  • 41
  • 138
  • 186
  • Have you tried it works with `Timecop.freeze(Time.new(2021, 10, 5, 9, 0, 0, '-07:00')) { }`? Maybe the RSpec integration is not properly setup? – Christian Bruckmayer Oct 28 '21 at 01:58
  • @ChristianBruckmayer I'm getting a Unitialized Constant error when trying to do this. `travel:` works so TimeCop is doing something but my tests are not aware of `TimeCop` apparently, so I need to properly set it up as you say. Any ideas would be helpful. Also, please submit an answer like spickerman did and I'll give you a +1 when I finally get this figured out. – aarona Oct 28 '21 at 15:33

1 Answers1

1

This is how Timecop#travel works and it is the expected behavior.

Setting a time to travel to sets the time to that timestamp but allows the time to move on. Because you have multiple Time.now calls in your test and the time moved on in between those calls those instances of time cannot be the same.

That means that you in your test must allow small differences in time between two instances, for example like this:

expect(Time.now).to be_within(1.second).of Time.now

Or you can freeze the time like Christian already mentioned in the comments. Freezing the time means that you set the current time to a specific time and it will not move on. For example like this:

before do
  Timecop.freeze(Time.new(2021, 10, 5, 9, 0, 0, '-07:00'))
end

after do
  Timecop.return
end

it "Testing TimeCop" do
  time = Time.new
  pause 10

  expect(time).to eq Time.now
end
spickermann
  • 100,941
  • 9
  • 101
  • 131
  • After I did some research yesterday (Thanks Christian) I had a feeling that this was the case with `Time.now`. However, I did try this yesterday before I ended the day and got an Uninit Const error on `TimeCop` which seems weird because `travel: some_time` works. I'm going to keep working on this. I'm sure this answer is correct but going to try and find out why my test is not aware of `TimeCop`. – aarona Oct 28 '21 at 15:30