wspurgin / rspec-sidekiq

RSpec for Sidekiq
https://github.com/wspurgin/rspec-sidekiq
Other
663 stars 132 forks source link

Checking for time fails #157

Closed coding-red-panda closed 4 years ago

coding-red-panda commented 6 years ago

Hello,

i'm writing specs to check that a job re-schedules itself one minute from the current time. I'm using the perform_in(1.minutes.from_now) along with the arguments.

The spec I am using to test his is as follows in RSpec:

it 'will reschedule itself in another minute' do
        subject.perform(survey_item_id: survey_item_id, project_id: project_id, job_id: job_id)
        expect(subject.class).to(
          have_enqueued_sidekiq_job(survey_item_id: survey_item_id, project_id: project_id, job_id: job_id)
            .at(1.minute.from_now)
        )
      end

This however fails with the following:

Failures:

  1) Jobs::V1::MachineLearning::StatusPolling#perform Job is Queued will reschedule itself in another minute
     Failure/Error:
       expect(subject.class).to(
         have_enqueued_sidekiq_job(survey_item_id: survey_item_id, project_id: project_id, job_id: job_id)
           .at(1.minute.from_now)
       )

       expected to have an enqueued Jobs::V1::MachineLearning::StatusPolling job
         arguments: [{"survey_item_id"=>16, "project_id"=>1337, "job_id"=>"5bc84c4c5ac8e00e79ef3ee7"}]
         options: {"at"=>Wed, 24 Oct 2018 12:22:37 UTC +00:00}
       found
         arguments: [[{"survey_item_id"=>16, "project_id"=>1337, "job_id"=>"5bc84c4c5ac8e00e79ef3ee7"}]]
         options: [{"at"=>1540383757.232822}]
     # ./spec/jobs/v1/machine_learning/status_polling_spec.rb:30:in `block (4 levels) in <top (required)>'

RSpec : 3.8 Sidekiq: 5.0.0

It seems as if different objects are used for this.

babasbot commented 4 years ago

In your example you're comparing Wed, 24 Oct 2018 12:22:37 UTC +00:00 with:

Time.at(1540383757.232822) # => 2018-10-24 07:22:37 -0500

@coding-bunny you could try the following:

it do
  worker = HardWorker.new
  worker.perform

  time = Time.at(1.minute.from_now)

  expect(HardWorker).to have_enqueued_sidekiq_job.at(time)
end

I also strongly suggest freezing the time with timecop or a similar library because your test may fail if it takes more than a second to reach the expectation statement.

I hope the answer helps 2 years after this issue was opened.

coding-red-panda commented 4 years ago

We actually swapped to using Timecop to resolve the problems. I should probably close this xD