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 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: callstack = caller.dup 68: callstack.slice!(0, 2) 69: ::ActiveSupport::Deprecation.warn("Using assert_redirected_to with partial hash arguments is deprecated. Specify the full set arguments instead", callstack) 70: return true 71: end 72: end 73: 74: redirected_to_after_normalisation = normalize_argument_to_redirection(@response.redirected_to) 75: options_after_normalisation = normalize_argument_to_redirection(options) 76: 77: if redirected_to_after_normalisation != options_after_normalisation 78: flunk "Expected response to be a redirect to <#{options_after_normalisation}> but was a redirect to <#{redirected_to_after_normalisation}>" 79: end 80: end 81: 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 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 actionpack/lib/action_controller/assertions/response_assertions.rb, line 99 99: def assert_template(options = {}, message = nil) 100: clean_backtrace do 101: case options 102: when NilClass, String, Symbol 103: rendered = @response.rendered[:template].to_s 104: msg = build_message(message, 105: "expecting <?> but rendering with <?>", 106: options, rendered) 107: assert_block(msg) do 108: if options.nil? 109: @response.rendered[:template].blank? 110: else 111: rendered.to_s.match(options.to_s) 112: end 113: end 114: when Hash 115: if expected_partial = options[:partial] 116: partials = @response.rendered[:partials] 117: if expected_count = options[:count] 118: found = partials.detect { |p, _| p.to_s.match(expected_partial) } 119: actual_count = found.nil? ? 0 : found.second 120: msg = build_message(message, 121: "expecting ? to be rendered ? time(s) but rendered ? time(s)", 122: expected_partial, expected_count, actual_count) 123: assert(actual_count == expected_count.to_i, msg) 124: else 125: msg = build_message(message, 126: "expecting partial <?> but action rendered <?>", 127: options[:partial], partials.keys) 128: assert(partials.keys.any? { |p| p.to_s.match(expected_partial) }, msg) 129: end 130: else 131: assert @response.rendered[:partials].empty?, 132: "Expected no partials to be rendered" 133: end 134: else 135: raise ArgumentError 136: end 137: end 138: end