Class: RSpec::Rails::Matchers::HaveHttpStatus::GenericStatus Private

Inherits:
BaseMatcher
  • Object
show all
Includes:
RSpec::Rails::Matchers::HaveHttpStatus
Defined in:
lib/rspec/rails/matchers/have_http_status.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Provides an implementation for have_http_status matching against ActionDispatch::TestResponse http status category queries.

Not intended to be instantiated directly.

Examples:

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)

See Also:

Constant Summary

Constants inherited from BaseMatcher

BaseMatcher::UNDEFINED

Class Method Summary collapse

Instance Method Summary collapse

Methods included from RSpec::Rails::Matchers::HaveHttpStatus

as_test_response, #invalid_response_type_message, matcher_for_status

Methods inherited from BaseMatcher

#diffable?, #expects_call_stack_jump?, #match_unless_raises, #supports_block_expectations?

Constructor Details

#initialize(type) ⇒ GenericStatus

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of GenericStatus.



255
256
257
258
259
260
261
262
263
# File 'lib/rspec/rails/matchers/have_http_status.rb', line 255

def initialize(type)
  unless self.class.valid_statuses.include?(type)
    raise ArgumentError, "Invalid generic HTTP status: #{type.inspect}"
  end

  @expected = type
  @actual = nil
  @invalid_response = nil
end

Class Method Details

.valid_statusesArray<Symbol>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns of status codes which represent a HTTP status code "group".

Returns:

  • (Array<Symbol>)

    of status codes which represent a HTTP status code "group"

See Also:



247
248
249
250
251
252
253
# File 'lib/rspec/rails/matchers/have_http_status.rb', line 247

def self.valid_statuses
  [
    :error, :success, :missing,
    :server_error, :successful, :not_found,
    :redirect
  ]
end

Instance Method Details

#descriptionString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (String)


277
278
279
# File 'lib/rspec/rails/matchers/have_http_status.rb', line 277

def description
  "respond with #{type_message}"
end

#failure_messageString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns explaining why the match failed.

Returns:

  • (String)

    explaining why the match failed



282
283
284
285
# File 'lib/rspec/rails/matchers/have_http_status.rb', line 282

def failure_message
  invalid_response_type_message ||
  "expected the response to have #{type_message} but it was #{actual}"
end

#failure_message_when_negatedString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns explaining why the match failed.

Returns:

  • (String)

    explaining why the match failed



288
289
290
291
# File 'lib/rspec/rails/matchers/have_http_status.rb', line 288

def failure_message_when_negated
  invalid_response_type_message ||
  "expected the response not to have #{type_message} but it was #{actual}"
end

#matches?(response) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns true if Rack's associated numeric HTTP code matched the response code or the named response status.

Returns:

  • (Boolean)

    true if Rack's associated numeric HTTP code matched the response code or the named response status



267
268
269
270
271
272
273
274
# File 'lib/rspec/rails/matchers/have_http_status.rb', line 267

def matches?(response)
  test_response = as_test_response(response)
  @actual = test_response.response_code
  check_expected_status(test_response, expected)
rescue TypeError => _ignored
  @invalid_response = response
  false
end