1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
|
Feature: Job specs
Job specs provide alternative assertions to those available in `ActiveJob::TestHelper` and help assert behaviour of the jobs themselves and that other entities correctly enqueue them.
Job specs are marked by `type: :job` or if you have set `config.infer_spec_type_from_file_location!` by placing them in `spec/jobs`.
With job specs, you can:
* specify the job class which was enqueued
* specify the arguments passed to the job
* specify when the job was enqueued until
* specify the queue which the job was enqueued to
Check the documentation on
[`have_been_enqueued`](../matchers/have-been-enqueued-matcher),
[`have_enqueued_job`](../matchers/have-enqueued-job-matcher),
[`have_been_performed`](../matchers/have-been-performed-matcher), and
[`have_performed_job`](../matchers/have-performed-job-matcher)
for more information.
Background:
Given active job is available
Scenario: Specify that job was enqueued
Given a file named "spec/jobs/upload_backups_job_spec.rb" with:
"""ruby
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
Scenario: Specify that job was enqueued for the correct date and time
Given a file named "spec/jobs/upload_backups_job_spec.rb" with:
"""ruby
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
Scenario: Specify that job was enqueued with no wait
Given a file named "spec/jobs/upload_backups_job_spec.rb" with:
"""ruby
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
Scenario: Specify that job was enqueued with a priority
Given a file named "spec/jobs/upload_backups_job_spec.rb" with:
"""ruby
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(priority: 5).perform_later
}.to have_enqueued_job.at_priority(5)
end
end
end
"""
When I run `rspec spec/jobs/upload_backups_job_spec.rb`
Then the example should pass
Scenario: Specify that job was enqueued with alias block syntax
Given a file named "spec/jobs/upload_backups_job_spec.rb" with:
"""ruby
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
Scenario: Specify that job was enqueued with imperative syntax
Given a file named "spec/jobs/upload_backups_job_spec.rb" with:
"""ruby
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
Scenario: Specify that job was enqueued with imperative syntax and a chained expectation
Given a file named "spec/jobs/upload_backups_job_spec.rb" with:
"""ruby
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
Scenario: The test adapter must be set to `:test`
Given a file named "spec/jobs/upload_backups_job_spec.rb" with:
"""ruby
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
|