A small suite of assertions that test responses from Rails applications.
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.
# 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 62 62: def assert_redirected_to(options = {}, message=nil) 63: validate_request! 64: 65: assert_response(:redirect, message) 66: return true if options == @response.location 67: 68: redirected_to_after_normalisation = normalize_argument_to_redirection(@response.location) 69: options_after_normalisation = normalize_argument_to_redirection(options) 70: 71: if redirected_to_after_normalisation != options_after_normalisation 72: flunk "Expected response to be a redirect to <#{options_after_normalisation}> but was a redirect to <#{redirected_to_after_normalisation}>" 73: end 74: 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 ActionDispatch::StatusCodes for a full list.
# 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 33 33: def assert_response(type, message = nil) 34: validate_request! 35: 36: if [ :success, :missing, :redirect, :error ].include?(type) && @response.send("#{type}?") 37: assert_block("") { true } # to count the assertion 38: elsif type.is_a?(Fixnum) && @response.response_code == type 39: assert_block("") { true } # to count the assertion 40: elsif type.is_a?(Symbol) && @response.response_code == Rack::Utils::SYMBOL_TO_STATUS_CODE[type] 41: assert_block("") { true } # to count the assertion 42: else 43: assert_block(build_message(message, "Expected response to be a <?>, but was <?>", type, @response.response_code)) { false } 44: end 45: end
# File lib/action_dispatch/testing/assertions/response.rb, line 82 82: def normalize_argument_to_redirection(fragment) 83: case fragment 84: when %{^\w[\w\d+.-]*:.*} 85: fragment 86: when String 87: if fragment =~ %{^\w[\w\d+.-]*:.*} 88: fragment 89: else 90: @request.protocol + @request.host_with_port + fragment 91: end 92: when :back 93: raise RedirectBackError unless refer = @request.headers["Referer"] 94: refer 95: else 96: @controller.url_for(fragment) 97: end.gsub(/[\r\n]/, '') 98: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.