Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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
like image 621
user3895395 Avatar asked May 17 '16 16:05

user3895395


People also ask

How do I run an RSpec on a specific file?

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.

How do I run a RSpec test in terminal?

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.


1 Answers

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
like image 66
thisismydesign Avatar answered Nov 09 '22 19:11

thisismydesign