Search code examples
ruby-on-railsrubysidekiqminitestrails-activejob

Testing ActiveJob with Minitest doesn't hit Sidekiq queue


On Rails 4.2 I have the following ActiveJob test :

test/jobs/import_job_test.rb

require 'test_helper'

class ImportJobTest < ActiveJob::TestCase

  def setup
    @response = ImportJob.perform_later "'testing Sidekiq queue jobs'"
  end

  test "enqueued jobs" do
    assert_enqueued_jobs 1
    clear_enqueued_jobs
    assert_enqueued_jobs 0
  end

  test "ActiveJob::QueueAdapters::SidekiqAdapter::JobWrapper" do
    assert_equal ["'testing Sidekiq queue jobs'"], @response.arguments
  end

  test "a second new job has been enqueued with the given arguments" do
    assert_enqueued_jobs 1
    assert_enqueued_with(job: ImportJob, args: ["'queuing a second job'"], queue: 'default') do
      ImportJob.perform_later "'queuing a second job'"
    end
    assert_enqueued_jobs 2
  end

end

Running the test it goes green:

$ rake test test/jobs/import_job_test.rb
Started with run options --seed 35322

  4/4: [===================================] 100% Time: 00:00:00, Time: 00:00:00

Finished in 0.01380s
4 tests, 7 assertions, 0 failures, 0 errors, 0 skips

but never touch the Sidekiq queue really. I get green also when Sidekiq is turned off, which I don't want. Of course running in console the queue is bitten.

How can I specify to REALLY hit the queue in test mode ?


Solution

  • The reason for using an API like ActiveJob is to allow you to write your code to an abstract API so you can change adapters. In other words, your ActiveJob classes should be able to move from Sidekiq to Que without making any changes to your code. Because of this, ActiveJob::TestCase uses a test adapter that makes asserting job behavior easier.

    That said, if you really want to make your jobs hit a running queue, you should configure your test environment accordingly and inherit from a test class that doesn't use ActiveJob::TestHelper.