Home > Net >  Why is ActiveJob InlineAdapter not used in request tests?
Why is ActiveJob InlineAdapter not used in request tests?

Time:11-07

I have this code in my test.rb :

config.active_job.queue_adapter = :inline

In the test I have:

scenario '15 minutes after last call a recall should happen' do    
  p ActiveJob::Base.queue_adapter
end

This returns: ActiveJob::QueueAdapters::InlineAdapter

which is good because the perform_later are executed immediately.

However when I add type: :request to the test like this:

scenario '15 minutes after last call a recall should happen', type: :request do    
  p ActiveJob::Base.queue_adapter
end

i'm getting: requestActiveJob::QueueAdapters::TestAdapter and the perform_later isn't executed anymore. Is this intended behaviour? How can I make sure the perform_later blocks are always executed in tests?

CodePudding user response:

Jobs are not enqueued to be performed, but they are enqueued to be checked if they were enqueued (basically just saved in an array so you can test if they are there):

https://edgeapi.rubyonrails.org/classes/ActiveJob/QueueAdapters/TestAdapter.html

Since you tagged this question with rspec, then there's perfect matcher for you: have_been_enqueued which can be used like this:

RSpec.describe UploadBackupsJob do
  it "matches with enqueued job" do
    ActiveJob::Base.queue_adapter = :test
    UploadBackupsJob.perform_later
    expect(UploadBackupsJob).to have_been_enqueued
  end
end

CodePudding user response:

This is related to this issue: https://github.com/rails/rails/issues/37270

Putting

(ActiveJob::Base.descendants << ActiveJob::Base).each(&:disable_test_adapter)

IN the actual test fixes it.

Another more cleaner option is to (if possible) change the test type into: type: :system and making sure your rspec-rails version is >= 4.1

  • Related