A small suite of assertions that test responses from Rails applications.
Methods
Public Instance methods
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
[ show source ]
# File vendor/rails/actionpack/lib/action_controller/assertions/response_assertions.rb, line 59
59: def assert_redirected_to(options = {}, message=nil)
60: clean_backtrace do
61: assert_response(:redirect, message)
62: return true if options == @response.redirected_to
63:
64: # Support partial arguments for hash redirections
65: if options.is_a?(Hash) && @response.redirected_to.is_a?(Hash)
66: if options.all? {|(key, value)| @response.redirected_to[key] == value}
67: ::ActiveSupport::Deprecation.warn("Using assert_redirected_to with partial hash arguments is deprecated. Specify the full set arguments instead", caller)
68: return true
69: end
70: end
71:
72: redirected_to_after_normalisation = normalize_argument_to_redirection(@response.redirected_to)
73: options_after_normalisation = normalize_argument_to_redirection(options)
74:
75: if redirected_to_after_normalisation != options_after_normalisation
76: flunk "Expected response to be a redirect to <#{options_after_normalisation}> but was a redirect to <#{redirected_to_after_normalisation}>"
77: end
78: end
79: end
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 ActionController::StatusCodes 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
[ show source ]
# File vendor/rails/actionpack/lib/action_controller/assertions/response_assertions.rb, line 24
24: def assert_response(type, message = nil)
25: clean_backtrace do
26: if [ :success, :missing, :redirect, :error ].include?(type) && @response.send("#{type}?")
27: assert_block("") { true } # to count the assertion
28: elsif type.is_a?(Fixnum) && @response.response_code == type
29: assert_block("") { true } # to count the assertion
30: elsif type.is_a?(Symbol) && @response.response_code == ActionController::StatusCodes::SYMBOL_TO_STATUS_CODE[type]
31: assert_block("") { true } # to count the assertion
32: else
33: if @response.error?
34: exception = @response.template.instance_variable_get(:@exception)
35: exception_message = exception && exception.message
36: assert_block(build_message(message, "Expected response to be a <?>, but was <?>\n<?>", type, @response.response_code, exception_message.to_s)) { false }
37: else
38: assert_block(build_message(message, "Expected response to be a <?>, but was <?>", type, @response.response_code)) { false }
39: end
40: end
41: end
42: end
Asserts that the request was rendered with the appropriate template file or partials
Examples
# assert that the "new" view template was rendered assert_template "new" # assert that the "new" view template was rendered with Symbol assert_template :new # assert that the "_customer" partial was rendered twice assert_template :partial => '_customer', :count => 2 # assert that no partials were rendered assert_template :partial => false
[ show source ]
# File vendor/rails/actionpack/lib/action_controller/assertions/response_assertions.rb, line 97
97: def assert_template(options = {}, message = nil)
98: clean_backtrace do
99: case options
100: when NilClass, String, Symbol
101: rendered = @response.rendered[:template].to_s
102: msg = build_message(message,
103: "expecting <?> but rendering with <?>",
104: options, rendered)
105: assert_block(msg) do
106: if options.nil?
107: @response.rendered[:template].blank?
108: else
109: rendered.to_s.match(options.to_s)
110: end
111: end
112: when Hash
113: if expected_partial = options[:partial]
114: partials = @response.rendered[:partials]
115: if expected_count = options[:count]
116: found = partials.detect { |p, _| p.to_s.match(expected_partial) }
117: actual_count = found.nil? ? 0 : found.second
118: msg = build_message(message,
119: "expecting ? to be rendered ? time(s) but rendered ? time(s)",
120: expected_partial, expected_count, actual_count)
121: assert(actual_count == expected_count.to_i, msg)
122: else
123: msg = build_message(message,
124: "expecting partial <?> but action rendered <?>",
125: options[:partial], partials.keys)
126: assert(partials.keys.any? { |p| p.to_s.match(expected_partial) }, msg)
127: end
128: else
129: assert @response.rendered[:partials].empty?,
130: "Expected no partials to be rendered"
131: end
132: else
133: raise ArgumentError
134: end
135: end
136: end