module RSpec::Rails::Matchers

@api public Container module for Rails specific matchers.

Public Instance Methods

be_a_new(model_class) click to toggle source

@api public Passes if actual is an instance of `model_class` and returns `false` for `persisted?`. Typically used to specify instance variables assigned to views by controller actions

Use the `with` method to specify the specific attributes to match on the new record.

@example

get :new
assigns(:thing).should be_a_new(Thing)

post :create, :thing => { :name => "Illegal Value" }
assigns(:thing).should be_a_new(Thing).with(:name => nil)
# File lib/rspec/rails/matchers/be_a_new.rb, line 77
def be_a_new(model_class)
  BeANew.new(model_class)
end
be_new_record() click to toggle source

@api public Passes if actual returns `false` for `persisted?`.

@example

get :new
expect(assigns(:thing)).to be_new_record
# File lib/rspec/rails/matchers/be_new_record.rb, line 25
def be_new_record
  BeANewRecord.new
end
be_valid(*args) click to toggle source

@api public Passes if the given model instance's `valid?` method is true, meaning all of the `ActiveModel::Validations` passed and no errors exist. If a message is not given, a default message is shown listing each error.

@example

thing = Thing.new
expect(thing).to be_valid
# File lib/rspec/rails/matchers/be_valid.rb, line 44
def be_valid(*args)
  BeValid.new(*args)
end
enqueue_job(job = nil)
Alias for: have_enqueued_job
have_been_enqueued() click to toggle source

@api public Passes if a job has been enqueued. May chain at_least, at_most or exactly to specify a number of times.

@example

before { ActiveJob::Base.queue_adapter.enqueued_jobs.clear }

HeavyLiftingJob.perform_later
expect(HeavyLiftingJob).to have_been_enqueued

HelloJob.perform_later
HeavyLiftingJob.perform_later
expect(HeavyLiftingJob).to have_been_enqueued.exactly(:once)

3.times { HelloJob.perform_later }
expect(HelloJob).to have_been_enqueued.at_least(2).times

HelloJob.perform_later
expect(HelloJob).to enqueue_job(HelloJob).at_most(:twice)

HelloJob.perform_later
HeavyLiftingJob.perform_later
expect(HelloJob).to have_been_enqueued
expect(HeavyLiftingJob).to have_been_enqueued

HelloJob.set(wait_until: Date.tomorrow.noon, queue: "low").perform_later(42)
expect(HelloJob).to have_been_enqueued.with(42).on_queue("low").at(Date.tomorrow.noon)
# File lib/rspec/rails/matchers/active_job.rb, line 236
def have_been_enqueued
  check_active_job_adapter
  ActiveJob::HaveBeenEnqueued.new
end
have_enqueued_job(job = nil) click to toggle source

@api public Passes if a job has been enqueued inside block. May chain at_least, at_most or exactly to specify a number of times.

@example

expect {
  HeavyLiftingJob.perform_later
}.to have_enqueued_job

# Using alias
expect {
  HeavyLiftingJob.perform_later
}.to enqueue_job

expect {
  HelloJob.perform_later
  HeavyLiftingJob.perform_later
}.to have_enqueued_job(HelloJob).exactly(:once)

expect {
  3.times { HelloJob.perform_later }
}.to have_enqueued_job(HelloJob).at_least(2).times

expect {
  HelloJob.perform_later
}.to have_enqueued_job(HelloJob).at_most(:twice)

expect {
  HelloJob.perform_later
  HeavyLiftingJob.perform_later
}.to have_enqueued_job(HelloJob).and have_enqueued_job(HeavyLiftingJob)

expect {
  HelloJob.set(wait_until: Date.tomorrow.noon, queue: "low").perform_later(42)
}.to have_enqueued_job.with(42).on_queue("low").at(Date.tomorrow.noon)
# File lib/rspec/rails/matchers/active_job.rb, line 204
def have_enqueued_job(job = nil)
  check_active_job_adapter
  ActiveJob::HaveEnqueuedJob.new(job)
end
Also aliased as: enqueue_job
have_http_status(target) click to toggle source

@api public Passes if `response` has a matching HTTP status code.

The following symbolic status codes are allowed:

  • `Rack::Utils::SYMBOL_TO_STATUS_CODE`

  • One of the defined `ActionDispatch::TestResponse` aliases:

    • `:error`

    • `:missing`

    • `:redirect`

    • `:success`

@example Accepts numeric and symbol statuses

expect(response).to have_http_status(404)
expect(response).to have_http_status(:created)
expect(response).to have_http_status(:success)
expect(response).to have_http_status(:error)
expect(response).to have_http_status(:missing)
expect(response).to have_http_status(:redirect)

@example Works with standard `response` objects and Capybara's `page`

expect(response).to have_http_status(404)
expect(page).to     have_http_status(:created)

@see github.com/rails/rails/blob/master/actionpack/lib/action_dispatch/testing/test_response.rb `ActionDispatch::TestResponse` @see github.com/rack/rack/blob/master/lib/rack/utils.rb `Rack::Utils::SYMBOL_TO_STATUS_CODE`

# File lib/rspec/rails/matchers/have_http_status.rb, line 358
def have_http_status(target)
  raise ArgumentError, "Invalid HTTP status: nil" unless target
  HaveHttpStatus.matcher_for_status(target)
end

Private Instance Methods

check_active_job_adapter() click to toggle source

@private

# File lib/rspec/rails/matchers/active_job.rb, line 244
def check_active_job_adapter
  return if ::ActiveJob::QueueAdapters::TestAdapter === ::ActiveJob::Base.queue_adapter
  raise StandardError, "To use ActiveJob matchers set `ActiveJob::Base.queue_adapter = :test`"
end