Skip to content

Commit 063f035

Browse files
sgerrandJonRowe
authored andcommitted
Adds documentation and feature for job specs (#1725)
* Add documentation for ActiveJob specs Updates existing documentation with a section on job specs and adds a related cucumber feature. * Add failing example for configuration error Job specs must be set up with the correct queue adapter, otherwise an exception will be thrown. Adding an additional step definition allows this scenario to be tested. * Updates top level documentation for job specs Applying [PR feedback](#1725 (comment)).
1 parent b5f1086 commit 063f035

File tree

3 files changed

+159
-0
lines changed

3 files changed

+159
-0
lines changed

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,28 @@ end
321321
For more information, see the [cucumber scenarios for mailer specs
322322
](https://relishapp.com/rspec/rspec-rails/v/3-4/docs/mailer-specs).
323323

324+
## Job specs
325+
326+
Tagging a context with the metadata `:type => :job` treats its examples as job
327+
specs. Typically these specs will live in `spec/jobs`.
328+
329+
```ruby
330+
require 'rails_helper'
331+
332+
RSpec.describe UploadBackupsJob, :type => :job do
333+
describe "#perform_later" do
334+
it "uploads a backup" do
335+
ActiveJob::Base.queue_adapter = :test
336+
UploadBackupsJob.perform_later('backup')
337+
expect(UploadBackupsJob).to have_been_enqueued
338+
end
339+
end
340+
end
341+
```
342+
343+
For more information, see the [cucumber scenarios for job specs
344+
](https://relishapp.com/rspec/rspec-rails/docs/job-specs).
345+
324346
## View specs
325347

326348
View specs default to residing in the `spec/views` folder. Tagging any context

features/job_specs/job_spec.feature

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
Feature: job spec
2+
3+
Job specs provide alternative assertions to those available in
4+
`ActiveJob::TestHelper` and help assert behaviour of the jobs themselves
5+
and that other entities correctly enqueue them.
6+
7+
Job specs are marked by `:type => :job` or if you have set
8+
`config.infer_spec_type_from_file_location!` by placing them in `spec/jobs`.
9+
10+
With job specs, you can:
11+
12+
* specify the job class which was enqueued
13+
* specify the arguments passed to the job
14+
* specify when the job was enqueued until
15+
* specify the queue which the job was enqueued to
16+
17+
Check the documentation on
18+
[`have_been_enqueued`](matchers/have-been-enqueued-matcher) and
19+
[`have_enqueued_job`](matchers/have-enqueued-job-matcher) for more
20+
information.
21+
22+
Background:
23+
Given active job is available
24+
25+
Scenario: specify that job was enqueued
26+
Given a file named "spec/jobs/upload_backups_spec.rb" with:
27+
"""ruby
28+
require "rails_helper"
29+
30+
RSpec.describe UploadBackupsJob, :type => :job do
31+
describe "#perform_later" do
32+
it "uploads a backup" do
33+
ActiveJob::Base.queue_adapter = :test
34+
expect {
35+
UploadBackupsJob.perform_later('backup')
36+
}.to have_enqueued_job
37+
end
38+
end
39+
end
40+
"""
41+
When I run `rspec spec/jobs/upload_backups_spec.rb`
42+
Then the example should pass
43+
44+
Scenario: specify that job was enqueued for the correct date and time
45+
Given a file named "spec/jobs/upload_backups_spec.rb" with:
46+
"""ruby
47+
require "rails_helper"
48+
49+
RSpec.describe UploadBackupsJob, :type => :job do
50+
describe "#perform_later" do
51+
it "uploads a backup" do
52+
ActiveJob::Base.queue_adapter = :test
53+
expect {
54+
UploadBackupsJob.set(wait_until: Date.tomorrow.noon, queue: "low").perform_later('backup')
55+
}.to have_enqueued_job.with('backup').on_queue("low").at(Date.tomorrow.noon)
56+
end
57+
end
58+
end
59+
"""
60+
When I run `rspec spec/jobs/upload_backups_spec.rb`
61+
Then the example should pass
62+
63+
Scenario: specify that job was enqueued with alias block syntax
64+
Given a file named "spec/jobs/upload_backups_spec.rb" with:
65+
"""ruby
66+
require "rails_helper"
67+
68+
RSpec.describe UploadBackupsJob, :type => :job do
69+
describe "#perform_later" do
70+
it "uploads a backup" do
71+
ActiveJob::Base.queue_adapter = :test
72+
expect {
73+
UploadBackupsJob.perform_later('backup')
74+
}.to enqueue_job
75+
end
76+
end
77+
end
78+
"""
79+
When I run `rspec spec/jobs/upload_backups_spec.rb`
80+
Then the example should pass
81+
82+
Scenario: specify that job was enqueued with imperative syntax
83+
Given a file named "spec/jobs/upload_backups_spec.rb" with:
84+
"""ruby
85+
require "rails_helper"
86+
87+
RSpec.describe UploadBackupsJob, :type => :job do
88+
describe "#perform_later" do
89+
it "uploads a backup" do
90+
ActiveJob::Base.queue_adapter = :test
91+
UploadBackupsJob.perform_later('backup')
92+
expect(UploadBackupsJob).to have_been_enqueued
93+
end
94+
end
95+
end
96+
"""
97+
When I run `rspec spec/jobs/upload_backups_spec.rb`
98+
Then the example should pass
99+
100+
Scenario: specify that job was enqueued with imperative syntax and a chained expectation
101+
Given a file named "spec/jobs/upload_backups_spec.rb" with:
102+
"""ruby
103+
require "rails_helper"
104+
105+
RSpec.describe UploadBackupsJob, :type => :job do
106+
describe "#perform_later" do
107+
it "uploads a backup" do
108+
ActiveJob::Base.queue_adapter = :test
109+
UploadBackupsJob.perform_later('backup')
110+
expect(UploadBackupsJob).to have_been_enqueued.exactly(:once)
111+
end
112+
end
113+
end
114+
"""
115+
When I run `rspec spec/jobs/upload_backups_spec.rb`
116+
Then the example should pass
117+
118+
Scenario: the test adapter must be set to :test
119+
Given a file named "spec/jobs/upload_backups_spec.rb" with:
120+
"""ruby
121+
require "rails_helper"
122+
123+
RSpec.describe UploadBackupsJob, :type => :job do
124+
describe "#perform_later" do
125+
it "uploads a backup" do
126+
ActiveJob::Base.queue_adapter = :development
127+
end
128+
end
129+
end
130+
"""
131+
When I run `rspec spec/jobs/upload_backups_spec.rb`
132+
Then the example should fail

features/step_definitions/additional_cli_steps.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111
step %q{the exit status should be 0}
1212
end
1313

14+
Then /^the example(s)? should( all)? fail/ do |_, _|
15+
step %q{the output should not contain "0 failures"}
16+
step %q{the exit status should not be 0}
17+
end
18+
1419
Given /active job is available/ do
1520
if !RSpec::Rails::FeatureCheck.has_active_job?
1621
pending "ActiveJob is not available"

0 commit comments

Comments
 (0)