Skip to content

Adds documentation and feature for job specs #1725

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 18, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,28 @@ end
For more information, see the [cucumber scenarios for mailer specs
](https://relishapp.com/rspec/rspec-rails/v/3-4/docs/mailer-specs).

## Job specs

Tagging a context with the metadata `:type => :job` treats its examples as job
specs. Typically these specs will live in `spec/jobs`.

```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
```

For more information, see the [cucumber scenarios for job specs
](https://relishapp.com/rspec/rspec-rails/docs/job-specs).
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This link doesn't currently work

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you'd like to fix this, I'd welcome a PR!, this ones already been merged tho.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I wasn't clear if there was some kind of a deploy that needed to happen to make the URL work so it wasn't clear to me what the fix would be.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, you're totally right, /cc @myronmarston how do we publish to relish again?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rake relish

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, one flaw in this plan, I don't have access!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've signed up with the usual email address if you want to add me so I can do this Myron.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just added you as a collaborator.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Link works now, thanks @myronmarston and @drewish


## View specs

View specs default to residing in the `spec/views` folder. Tagging any context
Expand Down
132 changes: 132 additions & 0 deletions features/job_specs/job_spec.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
Feature: job spec

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) and
[`have_enqueued_job`](matchers/have-enqueued-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_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_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_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_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_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_spec.rb`
Then the example should pass

Scenario: specify that job was enqueued with imperative syntax
Given a file named "spec/jobs/upload_backups_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_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_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_spec.rb`
Then the example should pass

Scenario: the test adapter must be set to :test
Given a file named "spec/jobs/upload_backups_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_spec.rb`
Then the example should fail
5 changes: 5 additions & 0 deletions features/step_definitions/additional_cli_steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
step %q{the exit status should be 0}
end

Then /^the example(s)? should( all)? fail/ do |_, _|
step %q{the output should not contain "0 failures"}
step %q{the exit status should not be 0}
end

Given /active job is available/ do
if !RSpec::Rails::FeatureCheck.has_active_job?
pending "ActiveJob is not available"
Expand Down