Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fix assert_redirected_to tests according to real-world usage.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4261 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit 51cd1aec00c182c6789f09f9bf40365934034bcd 1 parent 71a570f
@technoweenie technoweenie authored
View
5 actionpack/CHANGELOG
@@ -1,5 +1,10 @@
*SVN*
+* Fix assert_redirected_to tests according to real-world usage. Also, don't fail if you add an extra :controller option: [Rick]
+
+ redirect_to :action => 'new'
+ assert_redirected_to :controller => 'monkeys', :action => 'new'
+
* Cache CgiRequest#request_parameters so that multiple calls don't re-parse multipart data. [Rick]
* Diff compared routing options. Allow #assert_recognizes to take a second arg as a hash to specify optional request method [Rick]
View
26 actionpack/lib/action_controller/assertions.rb
@@ -73,14 +73,16 @@ def assert_response(type, message = nil)
def assert_redirected_to(options = {}, message=nil)
clean_backtrace do
assert_response(:redirect, message)
- ActionController::Routing::Routes.reload if ActionController::Routing::Routes.empty?
-
+ return true if options == @response.redirected_to
+ ActionController::Routing::Routes.reload if ActionController::Routing::Routes.empty?
+
begin
- url = {}
- f={
- :expected => options.is_a?(Symbol) ? @controller.send("hash_for_#{options}_url") : options.dup,
- :actual => @response.redirected_to.is_a?(Symbol) ? @controller.send("hash_for_#{@response.redirected_to}_url") : @response.redirected_to.dup
- }.each do |key, value|
+ url = {}
+ original = { :expected => options, :actual => @response.redirected_to.dup }
+ original.each do |key, value|
+ if value.is_a?(Symbol)
+ value = @controller.respond_to?(value, true) ? @controller.send(value) : @controller.send("hash_for_#{option}")
+ end
unless value.is_a?(Hash)
request = case value
when NilClass then nil
@@ -90,7 +92,15 @@ def assert_redirected_to(options = {}, message=nil)
value = request.path_parameters if request
end
- value.stringify_keys! if value.is_a?(Hash)
+ if value.is_a?(Hash) # stringify 2 levels of hash keys
+ value.stringify_keys!
+ value.values.select { |v| v.is_a?(Hash) }.collect { |v| v.stringify_keys! }
+ if key == :expected && value['controller'] == @controller.controller_name && original[:actual].is_a?(Hash)
+ original[:actual].stringify_keys!
+ value.delete('controller') if original[:actual]['controller'].nil? || original[:actual]['controller'] == value['controller']
+ end
+ end
+
if value.respond_to?(:[]) && value['controller']
if key == :actual && value['controller'].first != '/'
value['controller'] = ActionController::Routing.controller_relative_to(value['controller'], @controller.class.controller_path)
View
18 actionpack/test/controller/action_pack_assertions_test.rb
@@ -234,7 +234,7 @@ def test_assert_redirect_to_named_route
process :redirect_to_named_route
assert_redirected_to 'http://test.host/route_one'
assert_redirected_to route_one_url
- assert_redirected_to :route_one
+ assert_redirected_to :route_one_url
end
end
@@ -256,7 +256,7 @@ def test_assert_redirect_to_named_route_failure
assert_redirected_to route_two_url
end
assert_raise(Test::Unit::AssertionFailedError) do
- assert_redirected_to :route_two
+ assert_redirected_to :route_two_url
end
end
end
@@ -456,7 +456,7 @@ def test_follow_redirect
assert "Inconceivable!", @response.body
end
-
+
def test_follow_redirect_outside_current_action
process :redirect_to_controller
assert_redirected_to :controller => "elsewhere", :action => "flash_me"
@@ -464,6 +464,18 @@ def test_follow_redirect_outside_current_action
assert_raises(RuntimeError, "Can't follow redirects outside of current controller (elsewhere)") { follow_redirect }
end
+ def test_assert_redirection_fails_with_incorrect_controller
+ process :redirect_to_controller
+ assert_raise(Test::Unit::AssertionFailedError) do
+ assert_redirected_to :controller => "action_pack_assertions", :action => "flash_me"
+ end
+ end
+
+ def test_assert_redirection_with_extra_controller_option
+ get :redirect_to_action
+ assert_redirected_to :controller => 'action_pack_assertions', :action => "flash_me", :id => 1, :params => { :panda => 'fun' }
+ end
+
def test_redirected_to_url_leadling_slash
process :redirect_to_path
assert_redirected_to '/some/path'
Please sign in to comment.
Something went wrong with that request. Please try again.