Class Index [+]

Quicksearch

ActionDispatch::Assertions::ResponseAssertions

A small suite of assertions that test responses from Rails applications.

Public Instance Methods

assert_redirected_to(options = {}, message=nil) click to toggle source

Assert that the redirection options passed in match those of the redirect called in the latest action. This match can be partial, such that assert_redirected_to(:controller => "weblog") will also match the redirection of redirect_to(:controller => "weblog", :action => "show") and so on.

Examples

  # assert that the redirection was to the "index" action on the WeblogController
  assert_redirected_to :controller => "weblog", :action => "index"

  # assert that the redirection was to the named route login_url
  assert_redirected_to login_url

  # assert that the redirection was to the url for @customer
  assert_redirected_to @customer
    # File lib/action_dispatch/testing/assertions/response.rb, line 57
57:       def assert_redirected_to(options = {}, message=nil)
58:         assert_response(:redirect, message)
59:         return true if options == @response.location
60: 
61:         redirect_is       = normalize_argument_to_redirection(@response.location)
62:         redirect_expected = normalize_argument_to_redirection(options)
63: 
64:         if redirect_is != redirect_expected
65:           flunk "Expected response to be a redirect to <#{redirect_expected}> but was a redirect to <#{redirect_is}>"
66:         end
67:       end
assert_response(type, message = nil) click to toggle source

Asserts that the response is one of the following types:

  • :success - Status code was 200

  • :redirect - Status code was in the 300-399 range

  • :missing - Status code was 404

  • :error - Status code was in the 500-599 range

You can also pass an explicit status number like assert_response(501) or its symbolic equivalent assert_response(:not_implemented). See Rack::Utils::SYMBOL_TO_STATUS_CODE for a full list.

Examples

  # assert that the response was a redirection
  assert_response :redirect

  # assert that the response code was status code 401 (unauthorized)
  assert_response 401
    # File lib/action_dispatch/testing/assertions/response.rb, line 28
28:       def assert_response(type, message = nil)
29:         validate_request!
30: 
31:         if type.in?([:success, :missing, :redirect, :error]) && @response.send("#{type}?")
32:           assert_block("") { true } # to count the assertion
33:         elsif type.is_a?(Fixnum) && @response.response_code == type
34:           assert_block("") { true } # to count the assertion
35:         elsif type.is_a?(Symbol) && @response.response_code == Rack::Utils::SYMBOL_TO_STATUS_CODE[type]
36:           assert_block("") { true } # to count the assertion
37:         else
38:           flunk(build_message(message, "Expected response to be a <?>, but was <?>", type, @response.response_code))
39:         end
40:       end

Private Instance Methods

normalize_argument_to_redirection(fragment) click to toggle source
    # File lib/action_dispatch/testing/assertions/response.rb, line 75
75:         def normalize_argument_to_redirection(fragment)
76:           case fragment
77:           when %{^\w[A-Za-z\d+.-]*:.*}
78:             fragment
79:           when String
80:             @request.protocol + @request.host_with_port + fragment
81:           when :back
82:             raise RedirectBackError unless refer = @request.headers["Referer"]
83:             refer
84:           else
85:             @controller.url_for(fragment)
86:           end.gsub(/[\00\\r\n]/, '')
87:         end
parameterize(value) click to toggle source

Proxy to to_param if the object will respond to it.

    # File lib/action_dispatch/testing/assertions/response.rb, line 71
71:         def parameterize(value)
72:           value.respond_to?(:to_param) ? value.to_param : value
73:         end
validate_request!() click to toggle source
    # File lib/action_dispatch/testing/assertions/response.rb, line 89
89:         def validate_request!
90:           unless @request.is_a?(ActionDispatch::Request)
91:             raise ArgumentError, "@request must be an ActionDispatch::Request"
92:           end
93:         end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.