Skip to content

ActiveJob Matcher .at() fails with comparison of ActiveSupport::TimeWithZone and Time #2333

Closed
@hughkelsey

Description

@hughkelsey

What Ruby, Rails and RSpec versions are you using?

Ruby version: ruby 2.6.3p62
Rails version: Rails 6.0.2.2
RSpec version: RSpec 3.9

  • rspec-core 3.9.2
  • rspec-expectations 3.9.2
  • rspec-mocks 3.9.1
  • rspec-rails 4.0.0
  • rspec-support 3.9.3

Observed behaviour

I'm using timecop to freeze time and run a job that is delayed 5 seconds.

Failures:

  1) SayHiJob should send in 5 seconds
     Failure/Error: expect { SayHiJob.perform_in_five }.to have_enqueued_job(SayHiJob).at(time + 5)

       expected to enqueue exactly 1 jobs, at Wed, 13 May 2020 13:25:42 UTC +00:00, but enqueued 0
       Queued jobs:
         SayHiJob job with [[]], on queue default, at 2020-05-13 06:25:42 -0700
     # ./spec/jobs/say_hi_job_spec.rb:8:in `block (3 levels) in <top (required)>'
     # /Users/hkelsey/.rvm/gems/ruby-2.6.3/gems/timecop-0.9.1/lib/timecop/timecop.rb:201:in `travel'
     # /Users/hkelsey/.rvm/gems/ruby-2.6.3/gems/timecop-0.9.1/lib/timecop/timecop.rb:129:in `send_travel'
     # /Users/hkelsey/.rvm/gems/ruby-2.6.3/gems/timecop-0.9.1/lib/timecop/timecop.rb:51:in `freeze'
     # ./spec/jobs/say_hi_job_spec.rb:7:in `block (2 levels) in <top (required)>'

Finished in 0.0321 seconds (files took 0.87185 seconds to load)

Expected behaviour

The spec should pass as these are the same times.

This failure happened after upgrading from 4.0.0.beta3 and is likely related to this commit:
f345e82

[26] pry(main)> time = Time.current
=> Wed, 13 May 2020 13:40:46 UTC +00:00
[27] pry(main)> Time.at(time.to_f)
=> 2020-05-13 06:40:46 -0700
[28] pry(main)> time == Time.at(time.to_f)
=> false

Can you provide an example app?

https://github.com/hughkelsey/ActiveJobMatcherAt

The relevant code is:

class SayHiJob < ApplicationJob
  queue_as :default

  def perform(*args)
    # Do something later
  end

  def self.perform_in_five(*args)
    set(wait: 5).perform_later(args)
  end
end

require 'rails_helper'

RSpec.describe SayHiJob, type: :job do
  let(:time) { Time.current }
  it 'should send in 5 seconds' do
    ActiveJob::Base.queue_adapter = :test
    Timecop.freeze time do
      expect { SayHiJob.perform_in_five }.to have_enqueued_job(SayHiJob).at(time + 5)
    end
  end
end

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions