Search code examples
ruby-on-railsrspecrspec-railsrails-activejob

RSpec compare Job enqueued for a later time


I have a job called DisableInvitationJob. I call it like this:

DisableInvitationJob.set(wait: 7.days).perform_later(invitation: @invitation)

And when testing the call with RSpec:

it {
expect(DisableInvitationJob).to have_been_enqueued
.with(invitation: Invitation.last)
.on_queue("default")
.at(7.days.from_now.change(:usec => 0))
.exactly(:once)
}

It fails like this:

expected to enqueue exactly 1 jobs, with [{:invitation=>#<Invitation id: 16, email: "test@test.io", expiration_date: "2023-03-11", oauth_token: "Yxw6qi2mqpDaBztnjRTaQEvXJnb5ot9G3FmX", group_id: 10, sender_id: 41, recipient_id: nil, created_at: "2023-03-04 20:10:07.345964000 +0900", updated_at: "2023-03-04 20:10:07.346646000 +0900", disabled: false>}], on queue default, at 2023-03-11 20:10:07 +0900, but enqueued 0
       Queued jobs:
         DisableInvitationJob job with [{:invitation=>#<Invitation id: 16, email: "test@test.io", expiration_date: "2023-03-11", oauth_token: "Yxw6qi2mqpDaBztnjRTaQEvXJnb5ot9G3FmX", group_id: 10, sender_id: 41, recipient_id: nil, created_at: "2023-03-04 20:10:07.345964000 +0900", updated_at: "2023-03-04 20:10:07.346646000 +0900", disabled: false>}], on queue default, at 2023-03-11 20:10:07 +0900

What is failing is the at part of the test. Even though the queue dates are the same by the second.

If I remove it:

it {
expect(DisableInvitationJob).to have_been_enqueued
.with(invitation: Invitation.last)
.on_queue("default")
.exactly(:once)
}

It works. How can I test it with the at part?

Thanks!


Solution

  • In the end I used wait_until referencing the docs

    This is how I call the job

     DisableInvitationJob.set(wait_until: Date.tomorrow.noon + 7.days).perform_later(invitation: invitation)
    
    

    This is how I test it:

    it "enqueues a job to disable the invitation" do
     expect(DisableInvitationJob).to have_been_enqueued
     .with(invitation: Invitation.last)
     .on_queue("default")
     .at(Date.tomorrow.noon + 7.days)
     .exactly(:once)
    end