メインコンテンツまでスキップ

Job specs

仕事の仕様には、ActiveJob::TestHelperで利用可能なものとは異なる代替アサーションが提供され、仕事自体の振る舞いと他のエンティティがそれらを正しくエンキューしていることをアサートするのに役立ちます。

ジョブの仕様は、type: :jobとマークされるか、config.infer_spec_type_from_file_location!が設定されている場合はspec/jobsに配置されます。

ジョブの仕様では、次のことができます:

  • ジョブがエンキューされたクラスを指定する
  • ジョブに渡される引数を指定する
  • ジョブがエンキューされた時間を指定する
  • ジョブがエンキューされたキューを指定する

Check the documentation on have_been_enqueued, have_enqueued_job, have_been_performed, and have_performed_job for more information.

Background

Given active job is available.

Specify that job was enqueued

Given a file named "spec/jobs/upload_backups_job_spec.rb" with:

require "rails_helper"

RSpec.describe UploadBackupsJob, type: :job do
describe "#perform_later" do
it "uploads a backup" do
ActiveJob::Base.queue_adapter = :test
expect {
UploadBackupsJob.perform_later('backup')
}.to have_enqueued_job
end
end
end

When I run rspec spec/jobs/upload_backups_job_spec.rb

Then the example should pass.

Specify that job was enqueued for the correct date and time

Given a file named "spec/jobs/upload_backups_job_spec.rb" with:

require "rails_helper"

RSpec.describe UploadBackupsJob, type: :job do
describe "#perform_later" do
it "uploads a backup" do
ActiveJob::Base.queue_adapter = :test
expect {
UploadBackupsJob.set(wait_until: Date.tomorrow.noon, queue: "low").perform_later('backup')
}.to have_enqueued_job.with('backup').on_queue("low").at(Date.tomorrow.noon)
end
end
end

When I run rspec spec/jobs/upload_backups_job_spec.rb

Then the example should pass.

Specify that job was enqueued with no wait

Given a file named "spec/jobs/upload_backups_job_spec.rb" with:

require "rails_helper"

RSpec.describe UploadBackupsJob, type: :job do
describe "#perform_later" do
it "uploads a backup" do
ActiveJob::Base.queue_adapter = :test
expect {
UploadBackupsJob.set(queue: "low").perform_later('backup')
}.to have_enqueued_job.with('backup').on_queue("low").at(:no_wait)
end
end
end

When I run rspec spec/jobs/upload_backups_job_spec.rb

Then the example should pass.

Specify that job was enqueued with alias block syntax

Given a file named "spec/jobs/upload_backups_job_spec.rb" with:

require "rails_helper"

RSpec.describe UploadBackupsJob, type: :job do
describe "#perform_later" do
it "uploads a backup" do
ActiveJob::Base.queue_adapter = :test
expect {
UploadBackupsJob.perform_later('backup')
}.to enqueue_job
end
end
end

When I run rspec spec/jobs/upload_backups_job_spec.rb

Then the example should pass.

Specify that job was enqueued with imperative syntax

Given a file named "spec/jobs/upload_backups_job_spec.rb" with:

require "rails_helper"

RSpec.describe UploadBackupsJob, type: :job do
describe "#perform_later" do
it "uploads a backup" do
ActiveJob::Base.queue_adapter = :test
UploadBackupsJob.perform_later('backup')
expect(UploadBackupsJob).to have_been_enqueued
end
end
end

When I run rspec spec/jobs/upload_backups_job_spec.rb

Then the example should pass.

Specify that job was enqueued with imperative syntax and a chained expectation

Given a file named "spec/jobs/upload_backups_job_spec.rb" with:

require "rails_helper"

RSpec.describe UploadBackupsJob, type: :job do
describe "#perform_later" do
it "uploads a backup" do
ActiveJob::Base.queue_adapter = :test
UploadBackupsJob.perform_later('backup')
expect(UploadBackupsJob).to have_been_enqueued.exactly(:once)
end
end
end

When I run rspec spec/jobs/upload_backups_job_spec.rb

Then the example should pass.

The test adapter must be set to :test

Given a file named "spec/jobs/upload_backups_job_spec.rb" with:

require "rails_helper"

RSpec.describe UploadBackupsJob, type: :job do
describe "#perform_later" do
it "uploads a backup" do
ActiveJob::Base.queue_adapter = :development
end
end
end

When I run rspec spec/jobs/upload_backups_job_spec.rb

Then the example should fail.