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
Running tests by their file or directory names is the most familiar way to run tests with RSpec. RSpec can take a file name or directory name and run the file or the contents of the directory. So you can do: rspec spec/jobs to run the tests found in the jobs directory.
Open your terminal, cd into the project directory, and run rspec spec . The spec is the folder in which rspec will find the tests. You should see output saying something about “uninitialized constant Object::Book”; this just means there's no Book class.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With