Use with_routing helper in tests instead of modifying global route set
This commit is contained in:
parent
61411f2aeb
commit
86ed58d912
@ -46,14 +46,8 @@ def trailing_slash
|
|||||||
class PageCachingTest < ActionController::TestCase
|
class PageCachingTest < ActionController::TestCase
|
||||||
def setup
|
def setup
|
||||||
super
|
super
|
||||||
ActionController::Base.perform_caching = true
|
|
||||||
|
|
||||||
ActionController::Routing::Routes.draw do |map|
|
ActionController::Base.perform_caching = true
|
||||||
map.main '', :controller => 'posts', :format => nil
|
|
||||||
map.formatted_posts 'posts.:format', :controller => 'posts'
|
|
||||||
map.resources :posts
|
|
||||||
map.connect ':controller/:action/:id'
|
|
||||||
end
|
|
||||||
|
|
||||||
@request = ActionController::TestRequest.new
|
@request = ActionController::TestRequest.new
|
||||||
@request.host = 'hostname.com'
|
@request.host = 'hostname.com'
|
||||||
@ -74,10 +68,16 @@ def teardown
|
|||||||
end
|
end
|
||||||
|
|
||||||
def test_page_caching_resources_saves_to_correct_path_with_extension_even_if_default_route
|
def test_page_caching_resources_saves_to_correct_path_with_extension_even_if_default_route
|
||||||
@params[:format] = 'rss'
|
with_routing do |set|
|
||||||
assert_equal '/posts.rss', @rewriter.rewrite(@params)
|
set.draw do |map|
|
||||||
@params[:format] = nil
|
map.main '', :controller => 'posts', :format => nil
|
||||||
assert_equal '/', @rewriter.rewrite(@params)
|
map.formatted_posts 'posts.:format', :controller => 'posts'
|
||||||
|
end
|
||||||
|
@params[:format] = 'rss'
|
||||||
|
assert_equal '/posts.rss', @rewriter.rewrite(@params)
|
||||||
|
@params[:format] = nil
|
||||||
|
assert_equal '/', @rewriter.rewrite(@params)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_should_cache_get_with_ok_status
|
def test_should_cache_get_with_ok_status
|
||||||
|
@ -527,12 +527,6 @@ def setup
|
|||||||
super
|
super
|
||||||
ActionController::Base.use_accept_header = true
|
ActionController::Base.use_accept_header = true
|
||||||
@request.host = "www.example.com"
|
@request.host = "www.example.com"
|
||||||
|
|
||||||
ActionController::Routing::Routes.draw do |map|
|
|
||||||
map.resources :customers
|
|
||||||
map.resources :quiz_stores, :has_many => :customers
|
|
||||||
map.connect ":controller/:action/:id"
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def teardown
|
def teardown
|
||||||
@ -593,53 +587,59 @@ def test_using_resource
|
|||||||
end
|
end
|
||||||
|
|
||||||
def test_using_resource_for_post_with_html
|
def test_using_resource_for_post_with_html
|
||||||
post :using_resource
|
with_test_route_set do
|
||||||
assert_equal "text/html", @response.content_type
|
post :using_resource
|
||||||
assert_equal 302, @response.status
|
assert_equal "text/html", @response.content_type
|
||||||
assert_equal "http://www.example.com/customers/13", @response.location
|
assert_equal 302, @response.status
|
||||||
assert @response.redirect?
|
assert_equal "http://www.example.com/customers/13", @response.location
|
||||||
|
assert @response.redirect?
|
||||||
|
|
||||||
errors = { :name => :invalid }
|
errors = { :name => :invalid }
|
||||||
Customer.any_instance.stubs(:errors).returns(errors)
|
Customer.any_instance.stubs(:errors).returns(errors)
|
||||||
post :using_resource
|
post :using_resource
|
||||||
assert_equal "text/html", @response.content_type
|
assert_equal "text/html", @response.content_type
|
||||||
assert_equal 200, @response.status
|
assert_equal 200, @response.status
|
||||||
assert_equal "New world!\n", @response.body
|
assert_equal "New world!\n", @response.body
|
||||||
assert_nil @response.location
|
assert_nil @response.location
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_using_resource_for_post_with_xml
|
def test_using_resource_for_post_with_xml
|
||||||
@request.accept = "application/xml"
|
with_test_route_set do
|
||||||
|
@request.accept = "application/xml"
|
||||||
|
|
||||||
post :using_resource
|
post :using_resource
|
||||||
assert_equal "application/xml", @response.content_type
|
assert_equal "application/xml", @response.content_type
|
||||||
assert_equal 201, @response.status
|
assert_equal 201, @response.status
|
||||||
assert_equal "<name>david</name>", @response.body
|
assert_equal "<name>david</name>", @response.body
|
||||||
assert_equal "http://www.example.com/customers/13", @response.location
|
assert_equal "http://www.example.com/customers/13", @response.location
|
||||||
|
|
||||||
errors = { :name => :invalid }
|
errors = { :name => :invalid }
|
||||||
Customer.any_instance.stubs(:errors).returns(errors)
|
Customer.any_instance.stubs(:errors).returns(errors)
|
||||||
post :using_resource
|
post :using_resource
|
||||||
assert_equal "application/xml", @response.content_type
|
assert_equal "application/xml", @response.content_type
|
||||||
assert_equal 422, @response.status
|
assert_equal 422, @response.status
|
||||||
assert_equal errors.to_xml, @response.body
|
assert_equal errors.to_xml, @response.body
|
||||||
assert_nil @response.location
|
assert_nil @response.location
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_using_resource_for_put_with_html
|
def test_using_resource_for_put_with_html
|
||||||
put :using_resource
|
with_test_route_set do
|
||||||
assert_equal "text/html", @response.content_type
|
put :using_resource
|
||||||
assert_equal 302, @response.status
|
assert_equal "text/html", @response.content_type
|
||||||
assert_equal "http://www.example.com/customers/13", @response.location
|
assert_equal 302, @response.status
|
||||||
assert @response.redirect?
|
assert_equal "http://www.example.com/customers/13", @response.location
|
||||||
|
assert @response.redirect?
|
||||||
|
|
||||||
errors = { :name => :invalid }
|
errors = { :name => :invalid }
|
||||||
Customer.any_instance.stubs(:errors).returns(errors)
|
Customer.any_instance.stubs(:errors).returns(errors)
|
||||||
put :using_resource
|
put :using_resource
|
||||||
assert_equal "text/html", @response.content_type
|
assert_equal "text/html", @response.content_type
|
||||||
assert_equal 200, @response.status
|
assert_equal 200, @response.status
|
||||||
assert_equal "Edit world!\n", @response.body
|
assert_equal "Edit world!\n", @response.body
|
||||||
assert_nil @response.location
|
assert_nil @response.location
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_using_resource_for_put_with_xml
|
def test_using_resource_for_put_with_xml
|
||||||
@ -660,11 +660,13 @@ def test_using_resource_for_put_with_xml
|
|||||||
end
|
end
|
||||||
|
|
||||||
def test_using_resource_for_delete_with_html
|
def test_using_resource_for_delete_with_html
|
||||||
Customer.any_instance.stubs(:destroyed?).returns(true)
|
with_test_route_set do
|
||||||
delete :using_resource
|
Customer.any_instance.stubs(:destroyed?).returns(true)
|
||||||
assert_equal "text/html", @response.content_type
|
delete :using_resource
|
||||||
assert_equal 302, @response.status
|
assert_equal "text/html", @response.content_type
|
||||||
assert_equal "http://www.example.com/customers", @response.location
|
assert_equal 302, @response.status
|
||||||
|
assert_equal "http://www.example.com/customers", @response.location
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_using_resource_for_delete_with_xml
|
def test_using_resource_for_delete_with_xml
|
||||||
@ -685,21 +687,23 @@ def test_using_resource_with_parent_for_get
|
|||||||
end
|
end
|
||||||
|
|
||||||
def test_using_resource_with_parent_for_post
|
def test_using_resource_with_parent_for_post
|
||||||
@request.accept = "application/xml"
|
with_test_route_set do
|
||||||
|
@request.accept = "application/xml"
|
||||||
|
|
||||||
post :using_resource_with_parent
|
post :using_resource_with_parent
|
||||||
assert_equal "application/xml", @response.content_type
|
assert_equal "application/xml", @response.content_type
|
||||||
assert_equal 201, @response.status
|
assert_equal 201, @response.status
|
||||||
assert_equal "<name>david</name>", @response.body
|
assert_equal "<name>david</name>", @response.body
|
||||||
assert_equal "http://www.example.com/quiz_stores/11/customers/13", @response.location
|
assert_equal "http://www.example.com/quiz_stores/11/customers/13", @response.location
|
||||||
|
|
||||||
errors = { :name => :invalid }
|
errors = { :name => :invalid }
|
||||||
Customer.any_instance.stubs(:errors).returns(errors)
|
Customer.any_instance.stubs(:errors).returns(errors)
|
||||||
post :using_resource
|
post :using_resource
|
||||||
assert_equal "application/xml", @response.content_type
|
assert_equal "application/xml", @response.content_type
|
||||||
assert_equal 422, @response.status
|
assert_equal 422, @response.status
|
||||||
assert_equal errors.to_xml, @response.body
|
assert_equal errors.to_xml, @response.body
|
||||||
assert_nil @response.location
|
assert_nil @response.location
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_using_resource_with_collection
|
def test_using_resource_with_collection
|
||||||
@ -773,6 +777,18 @@ def test_not_acceptable
|
|||||||
get :default_overwritten
|
get :default_overwritten
|
||||||
assert_equal 406, @response.status
|
assert_equal 406, @response.status
|
||||||
end
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
def with_test_route_set
|
||||||
|
with_routing do |set|
|
||||||
|
set.draw do |map|
|
||||||
|
map.resources :customers
|
||||||
|
map.resources :quiz_stores, :has_many => :customers
|
||||||
|
map.connect ":controller/:action/:id"
|
||||||
|
end
|
||||||
|
yield
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class AbstractPostController < ActionController::Base
|
class AbstractPostController < ActionController::Base
|
||||||
|
@ -111,13 +111,6 @@ def unconditional_redirect
|
|||||||
|
|
||||||
tests TestController
|
tests TestController
|
||||||
|
|
||||||
setup do
|
|
||||||
ActionController::Routing::Routes.draw do |map|
|
|
||||||
map.foo '/foo', :controller => 'test', :action => 'foo'
|
|
||||||
map.connect ":controller/:action/:id"
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_using_symbol_back_with_no_referrer
|
def test_using_symbol_back_with_no_referrer
|
||||||
assert_raise(ActionController::RedirectBackError) { get :guarded_with_back }
|
assert_raise(ActionController::RedirectBackError) { get :guarded_with_back }
|
||||||
end
|
end
|
||||||
@ -130,8 +123,14 @@ def test_using_symbol_back_redirects_to_referrer
|
|||||||
|
|
||||||
def test_no_deprecation_warning_for_named_route
|
def test_no_deprecation_warning_for_named_route
|
||||||
assert_not_deprecated do
|
assert_not_deprecated do
|
||||||
get :guarded_one_for_named_route_test, :two => "not one"
|
with_routing do |set|
|
||||||
assert_redirected_to '/foo'
|
set.draw do |map|
|
||||||
|
map.foo '/foo', :controller => 'test', :action => 'foo'
|
||||||
|
map.connect ":controller/:action/:id"
|
||||||
|
end
|
||||||
|
get :guarded_one_for_named_route_test, :two => "not one"
|
||||||
|
assert_redirected_to '/foo'
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -19,32 +19,41 @@ def link_to_person(person)
|
|||||||
end
|
end
|
||||||
|
|
||||||
class PeopleHelperTest < ActionView::TestCase
|
class PeopleHelperTest < ActionView::TestCase
|
||||||
def setup
|
|
||||||
super
|
|
||||||
ActionController::Routing::Routes.draw do |map|
|
|
||||||
map.people 'people', :controller => 'people', :action => 'index'
|
|
||||||
map.connect ':controller/:action/:id'
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_title
|
def test_title
|
||||||
assert_equal "<h1>Ruby on Rails</h1>", title("Ruby on Rails")
|
assert_equal "<h1>Ruby on Rails</h1>", title("Ruby on Rails")
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_homepage_path
|
def test_homepage_path
|
||||||
assert_equal "/people", homepage_path
|
with_test_route_set do
|
||||||
|
assert_equal "/people", homepage_path
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_homepage_url
|
def test_homepage_url
|
||||||
assert_equal "http://test.host/people", homepage_url
|
with_test_route_set do
|
||||||
|
assert_equal "http://test.host/people", homepage_url
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_link_to_person
|
def test_link_to_person
|
||||||
person = mock(:name => "David")
|
with_test_route_set do
|
||||||
person.class.extend ActiveModel::Naming
|
person = mock(:name => "David")
|
||||||
expects(:mocha_mock_path).with(person).returns("/people/1")
|
person.class.extend ActiveModel::Naming
|
||||||
assert_equal '<a href="/people/1">David</a>', link_to_person(person)
|
expects(:mocha_mock_path).with(person).returns("/people/1")
|
||||||
|
assert_equal '<a href="/people/1">David</a>', link_to_person(person)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
def with_test_route_set
|
||||||
|
with_routing do |set|
|
||||||
|
set.draw do |map|
|
||||||
|
map.people 'people', :controller => 'people', :action => 'index'
|
||||||
|
map.connect ':controller/:action/:id'
|
||||||
|
end
|
||||||
|
yield
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class CrazyHelperTest < ActionView::TestCase
|
class CrazyHelperTest < ActionView::TestCase
|
||||||
|
Loading…
Reference in New Issue
Block a user