1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Raise Assertion instead of RoutingError for routing assertion failures.

Before this change, assert_recognizes, assert_generates, and
assert_routing raised ActionController::RoutingError when they failed to
recognize the route.

This commit changes them to raise Assertion instead. This aligns with
convention for logical failures, and supports reporting tools that care
about the difference between logical failures and errors e.g. the
summary at the end of a test run.

- Fixes #5899
This commit is contained in:
David Chelimsky 2012-05-19 16:28:14 -05:00
parent 3655d66eda
commit dcce01132d
4 changed files with 27 additions and 18 deletions

View file

@ -186,6 +186,9 @@
* `ActionView::Helpers::TextHelper#highlight` now defaults to the * `ActionView::Helpers::TextHelper#highlight` now defaults to the
HTML5 `mark` element. *Brian Cardarella* HTML5 `mark` element. *Brian Cardarella*
* `assert_generates`, `assert_recognizes`, and `assert_routing` all raise
`Assertion` instead of `RoutingError` *David Chelimsky*
## Rails 3.2.3 (March 30, 2012) ## ## Rails 3.2.3 (March 30, 2012) ##

View file

@ -69,11 +69,9 @@ module ActionDispatch
# assert_generates "changesets/12", { :controller => 'scm', :action => 'show_diff', :revision => "12" } # assert_generates "changesets/12", { :controller => 'scm', :action => 'show_diff', :revision => "12" }
def assert_generates(expected_path, options, defaults={}, extras = {}, message=nil) def assert_generates(expected_path, options, defaults={}, extras = {}, message=nil)
if expected_path =~ %r{://} if expected_path =~ %r{://}
begin fail_on(URI::InvalidURIError) do
uri = URI.parse(expected_path) uri = URI.parse(expected_path)
expected_path = uri.path.to_s.empty? ? "/" : uri.path expected_path = uri.path.to_s.empty? ? "/" : uri.path
rescue URI::InvalidURIError => e
raise ActionController::RoutingError, e.message
end end
else else
expected_path = "/#{expected_path}" unless expected_path.first == '/' expected_path = "/#{expected_path}" unless expected_path.first == '/'
@ -189,14 +187,12 @@ module ActionDispatch
request = ActionController::TestRequest.new request = ActionController::TestRequest.new
if path =~ %r{://} if path =~ %r{://}
begin fail_on(URI::InvalidURIError) do
uri = URI.parse(path) uri = URI.parse(path)
request.env["rack.url_scheme"] = uri.scheme || "http" request.env["rack.url_scheme"] = uri.scheme || "http"
request.host = uri.host if uri.host request.host = uri.host if uri.host
request.port = uri.port if uri.port request.port = uri.port if uri.port
request.path = uri.path.to_s.empty? ? "/" : uri.path request.path = uri.path.to_s.empty? ? "/" : uri.path
rescue URI::InvalidURIError => e
raise ActionController::RoutingError, e.message
end end
else else
path = "/#{path}" unless path.first == "/" path = "/#{path}" unless path.first == "/"
@ -205,11 +201,21 @@ module ActionDispatch
request.request_method = method if method request.request_method = method if method
params = @routes.recognize_path(path, { :method => method, :extras => extras }) params = fail_on(ActionController::RoutingError) do
@routes.recognize_path(path, { :method => method, :extras => extras })
end
request.path_parameters = params.with_indifferent_access request.path_parameters = params.with_indifferent_access
request request
end end
def fail_on(exception_class)
begin
yield
rescue exception_class => e
raise MiniTest::Assertion, e.message
end
end
end end
end end
end end

View file

@ -109,7 +109,7 @@ class ResourcesTest < ActionController::TestCase
expected_options = {:controller => 'messages', :action => 'show', :id => '1.1.1'} expected_options = {:controller => 'messages', :action => 'show', :id => '1.1.1'}
with_restful_routing :messages do with_restful_routing :messages do
assert_raise(ActionController::RoutingError) do assert_raise(Assertion) do
assert_recognizes(expected_options, :path => 'messages/1.1.1', :method => :get) assert_recognizes(expected_options, :path => 'messages/1.1.1', :method => :get)
end end
end end
@ -660,15 +660,15 @@ class ResourcesTest < ActionController::TestCase
options = { :controller => controller_name.to_s } options = { :controller => controller_name.to_s }
collection_path = "/#{controller_name}" collection_path = "/#{controller_name}"
assert_raise(ActionController::RoutingError) do assert_raise(Assertion) do
assert_recognizes(options.merge(:action => 'update'), :path => collection_path, :method => :patch) assert_recognizes(options.merge(:action => 'update'), :path => collection_path, :method => :patch)
end end
assert_raise(ActionController::RoutingError) do assert_raise(Assertion) do
assert_recognizes(options.merge(:action => 'update'), :path => collection_path, :method => :put) assert_recognizes(options.merge(:action => 'update'), :path => collection_path, :method => :put)
end end
assert_raise(ActionController::RoutingError) do assert_raise(Assertion) do
assert_recognizes(options.merge(:action => 'destroy'), :path => collection_path, :method => :delete) assert_recognizes(options.merge(:action => 'destroy'), :path => collection_path, :method => :delete)
end end
end end
@ -1353,7 +1353,7 @@ class ResourcesTest < ActionController::TestCase
end end
def assert_not_recognizes(expected_options, path) def assert_not_recognizes(expected_options, path)
assert_raise ActionController::RoutingError, Assertion do assert_raise Assertion do
assert_recognizes(expected_options, path) assert_recognizes(expected_options, path)
end end
end end

View file

@ -54,21 +54,21 @@ class RoutingAssertionsTest < ActionController::TestCase
end end
def test_assert_recognizes_with_hash_constraint def test_assert_recognizes_with_hash_constraint
assert_raise(ActionController::RoutingError) do assert_raise(Assertion) do
assert_recognizes({ :controller => 'secure_articles', :action => 'index' }, 'http://test.host/secure/articles') assert_recognizes({ :controller => 'secure_articles', :action => 'index' }, 'http://test.host/secure/articles')
end end
assert_recognizes({ :controller => 'secure_articles', :action => 'index', :protocol => 'https://' }, 'https://test.host/secure/articles') assert_recognizes({ :controller => 'secure_articles', :action => 'index', :protocol => 'https://' }, 'https://test.host/secure/articles')
end end
def test_assert_recognizes_with_block_constraint def test_assert_recognizes_with_block_constraint
assert_raise(ActionController::RoutingError) do assert_raise(Assertion) do
assert_recognizes({ :controller => 'block_articles', :action => 'index' }, 'http://test.host/block/articles') assert_recognizes({ :controller => 'block_articles', :action => 'index' }, 'http://test.host/block/articles')
end end
assert_recognizes({ :controller => 'block_articles', :action => 'index' }, 'https://test.host/block/articles') assert_recognizes({ :controller => 'block_articles', :action => 'index' }, 'https://test.host/block/articles')
end end
def test_assert_recognizes_with_query_constraint def test_assert_recognizes_with_query_constraint
assert_raise(ActionController::RoutingError) do assert_raise(Assertion) do
assert_recognizes({ :controller => 'query_articles', :action => 'index', :use_query => 'false' }, '/query/articles', { :use_query => 'false' }) assert_recognizes({ :controller => 'query_articles', :action => 'index', :use_query => 'false' }, '/query/articles', { :use_query => 'false' })
end end
assert_recognizes({ :controller => 'query_articles', :action => 'index', :use_query => 'true' }, '/query/articles', { :use_query => 'true' }) assert_recognizes({ :controller => 'query_articles', :action => 'index', :use_query => 'true' }, '/query/articles', { :use_query => 'true' })
@ -87,14 +87,14 @@ class RoutingAssertionsTest < ActionController::TestCase
end end
def test_assert_routing_with_hash_constraint def test_assert_routing_with_hash_constraint
assert_raise(ActionController::RoutingError) do assert_raise(Assertion) do
assert_routing('http://test.host/secure/articles', { :controller => 'secure_articles', :action => 'index' }) assert_routing('http://test.host/secure/articles', { :controller => 'secure_articles', :action => 'index' })
end end
assert_routing('https://test.host/secure/articles', { :controller => 'secure_articles', :action => 'index', :protocol => 'https://' }) assert_routing('https://test.host/secure/articles', { :controller => 'secure_articles', :action => 'index', :protocol => 'https://' })
end end
def test_assert_routing_with_block_constraint def test_assert_routing_with_block_constraint
assert_raise(ActionController::RoutingError) do assert_raise(Assertion) do
assert_routing('http://test.host/block/articles', { :controller => 'block_articles', :action => 'index' }) assert_routing('http://test.host/block/articles', { :controller => 'block_articles', :action => 'index' })
end end
assert_routing('https://test.host/block/articles', { :controller => 'block_articles', :action => 'index' }) assert_routing('https://test.host/block/articles', { :controller => 'block_articles', :action => 'index' })
@ -107,7 +107,7 @@ class RoutingAssertionsTest < ActionController::TestCase
end end
assert_routing('/artikel', :controller => 'articles', :action => 'index') assert_routing('/artikel', :controller => 'articles', :action => 'index')
assert_raise(ActionController::RoutingError) do assert_raise(Assertion) do
assert_routing('/articles', { :controller => 'articles', :action => 'index' }) assert_routing('/articles', { :controller => 'articles', :action => 'index' })
end end
end end