Module: RSpec::Rails::Matchers

Included in:
RailsExampleGroup
Defined in:
lib/rspec/rails/matchers.rb,
lib/rspec/rails/matchers/be_valid.rb,
lib/rspec/rails/matchers/be_a_new.rb,
lib/rspec/rails/matchers/active_job.rb,
lib/rspec/rails/matchers/redirect_to.rb,
lib/rspec/rails/matchers/be_new_record.rb,
lib/rspec/rails/matchers/have_rendered.rb,
lib/rspec/rails/matchers/routing_matchers.rb,
lib/rspec/rails/matchers/have_http_status.rb

Overview

Container module for Rails specific matchers.

Defined Under Namespace

Modules: ActiveJob, HaveHttpStatus, RedirectTo, RenderTemplate, RoutingMatchers Classes: BeANew

Instance Method Summary collapse

Instance Method Details

#be_a_new(model_class) ⇒ Object

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.

Examples:

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)


77
78
79
# File 'lib/rspec/rails/matchers/be_a_new.rb', line 77

def be_a_new(model_class)
  BeANew.new(model_class)
end

#be_new_recordObject

Passes if actual returns false for persisted?.

Examples:

get :new
expect(assigns(:thing)).to be_new_record


25
26
27
# File 'lib/rspec/rails/matchers/be_new_record.rb', line 25

def be_new_record
  BeANewRecord.new
end

#be_valid(*args) ⇒ Object

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.

Examples:

thing = Thing.new
expect(thing).to be_valid


44
45
46
# File 'lib/rspec/rails/matchers/be_valid.rb', line 44

def be_valid(*args)
  BeValid.new(*args)
end

#have_enqueued_job(job = nil) ⇒ Object

Passess if count of jobs were enqueued inside block

Examples:

expect {
  HeavyLiftingJob.perform_later
}.to have_enqueued_job

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

expect {
  HelloJob.perform_later
  HelloJob.perform_later
  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)


167
168
169
170
171
172
# File 'lib/rspec/rails/matchers/active_job.rb', line 167

def have_enqueued_job(job = nil)
  unless ::ActiveJob::QueueAdapters::TestAdapter === ::ActiveJob::Base.queue_adapter
    raise StandardError, "To use have_enqueued_job matcher set `ActiveJob::Base.queue_adapter = :test`"
  end
  ActiveJob::HaveEnqueuedJob.new(job)
end

#have_http_status(target) ⇒ Object

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

Examples:

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)

Works with standard response objects and Capybara's page

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

Raises:

  • (ArgumentError)

See Also:



358
359
360
361
# 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