Rails 7 adds the possibility to check on :priority for ActiveJob tests

ActiveJob was introduced so that all Rails apps have the adequate queuing infrastructure in place. It is not dependent on the actual queue adapter that is selected, allowing for easy switching between queue infrastructures in the future.

It means that queue configurations become the responsibility of the queue adapter and not ActiveJob itself. Hence, ActiveJob allows for priority levels to be set if the queue adapter supports it.

While this works great in building queueing systems, it often hinders writing tests for ActiveJob. However, the Rails team is working towards unifying that!

Before

When writing ActiveJob tests, only minimal configurations were testable, such as the time to queue and queue name. It was not possible to write tests for jobs that make use of the priority parameter.

test "job is enqueued" do
  assert_enqueued_with job: SampleJob, args: @user, priority: 5 do
    SampleJob.perform_later(@user)
  end
end

It would throw a parameter priority not found error.

After

With Rails 7, we now can test with the priority parameter for both assert_enqueued_with and assert_performed_with test helper methods.

test "job is enqueued" do
  assert_enqueued_with(job: SampleJob, priority: 10) do
    SampleJob.perform_later(@user)
  end
end

Need help on your Ruby on Rails or React project?

Join Our Newsletter