rspec rails testing: how can I force ActiveJob job's to run inline for certain tests?

喜你入骨 提交于 2019-12-23 08:06:34

问题


I would like my background jobs to run inline for certain marked tests. I can do it by wrapping the test with perform_enqueued do but I'd like to just be able to tag them with metadata and it happens automatically, if possible.

I've tried the following:

it "does everything in the job too", perform_enqueued: true do
end

config.around(:each) do |example|
  if example.metadata[:perform_enqueued]
    perform_enqueued_jobs do
      example.run
    end
  end
end

but it results in an error:

undefined method `perform_enqueued_jobs=' for ActiveJob::QueueAdapters::InlineAdapter:Class

回答1:


You need to set your adapter for tests to ActiveJob::QueueAdapters::TestAdapter which responds to .perform_enqueued_jobs =. You can do that on your spec/rails_helper.rb file:

ActiveJob::Base.queue_adapter = :test



回答2:


In your spec/rails_helper.rb:

RSpec.configure do |config|
  # ...
  config.include ActiveJob::TestHelper
end

Or in your test:

context "when jobs are executed" do
  include ActiveJob::TestHelper

  # ...
end

Then in your tests:

perform_enqueued_jobs do
  example.run
end


来源:https://stackoverflow.com/questions/37281665/rspec-rails-testing-how-can-i-force-activejob-jobs-to-run-inline-for-certain-t

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!