问题
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