Last active
April 6, 2020 20:17
-
-
Save wrburgess/d7ecfef44499022846f5d112dc04df2f to your computer and use it in GitHub Desktop.
ActiveJob on Rails 5 with RSpec
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# app/jobs/example_job.rb | |
class ExampleJob < ActiveJob::Base | |
queue_as :default | |
rescue_from(ActiveRecord::RecordNotFound) do | |
retry_job wait: 1.minute, queue: :default | |
end | |
def perform(param_1, param_2) | |
User.create(email: "[email protected]", password: "12345678", password_confirmation: "12345678") | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# spec/jobs/example_job_spec.rb | |
require "rails_helper" | |
RSpec.describe ExampleJob, type: :job do | |
include ActiveJob::TestHelper | |
subject(:job) { described_class.perform_later("param_1", "param_2") } | |
it "queues the job" do | |
expect { job }.to change(ActiveJob::Base.queue_adapter.enqueued_jobs, :size).by(1) | |
end | |
it "matches with enqueued job" do | |
expect { described_class.perform_later }.to have_enqueued_job(described_class) | |
end | |
it "is in default queue" do | |
expect(described_class.new.queue_name).to eq("default") | |
end | |
it "executes perform" do | |
perform_enqueued_jobs { job } | |
expect(User.count).to eq 1 | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
RSpec.configure do |config| | |
# ... | |
config.after(:each, type: :job) do | |
clear_enqueued_jobs | |
clear_performed_jobs | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment