Method: RSpec::Rails::Matchers#have_http_status

Defined in:
lib/rspec/rails/matchers/have_http_status.rb

#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:



378
379
380
381
382
# File 'lib/rspec/rails/matchers/have_http_status.rb', line 378

def have_http_status(target)
  raise ArgumentError, "Invalid HTTP status: nil" unless target

  HaveHttpStatus.matcher_for_status(target)
end