Skip to content

Commit ea2bb23

Browse files
authored
Merge pull request rails#50727 from seanpdoyle/action-view-tests-simplify-routing
Action View Tests: Use `#with_routing` helper
2 parents d60a234 + 15ddb5a commit ea2bb23

File tree

2 files changed

+21
-59
lines changed

2 files changed

+21
-59
lines changed

actionview/test/abstract_unit.rb

Lines changed: 12 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -72,19 +72,6 @@ def render_erb(string)
7272
end
7373
end
7474

75-
class RoutedRackApp
76-
attr_reader :routes
77-
78-
def initialize(routes, &blk)
79-
@routes = routes
80-
@stack = ActionDispatch::MiddlewareStack.new(&blk).build(@routes)
81-
end
82-
83-
def call(env)
84-
@stack.call(env)
85-
end
86-
end
87-
8875
class BasicController
8976
attr_accessor :request, :response
9077

@@ -102,30 +89,13 @@ def config
10289
end
10390

10491
class ActionDispatch::IntegrationTest < ActiveSupport::TestCase
105-
def self.build_app(routes = nil)
106-
routes ||= ActionDispatch::Routing::RouteSet.new.tap { |rs|
107-
rs.draw { }
108-
}
109-
RoutedRackApp.new(routes) do |middleware|
110-
middleware.use ActionDispatch::ShowExceptions, ActionDispatch::PublicExceptions.new("#{FIXTURE_LOAD_PATH}/public")
111-
middleware.use ActionDispatch::DebugExceptions
112-
middleware.use ActionDispatch::Callbacks
113-
middleware.use ActionDispatch::Cookies
114-
middleware.use ActionDispatch::Flash
115-
middleware.use Rack::Head
116-
yield(middleware) if block_given?
117-
end
118-
end
119-
120-
self.app = build_app
121-
122-
def with_routing(&block)
123-
temporary_routes = ActionDispatch::Routing::RouteSet.new
124-
old_app, self.class.app = self.class.app, self.class.build_app(temporary_routes)
125-
126-
yield temporary_routes
127-
ensure
128-
self.class.app = old_app
92+
self.app = ActionDispatch::MiddlewareStack.new do |middleware|
93+
middleware.use ActionDispatch::ShowExceptions, ActionDispatch::PublicExceptions.new("#{FIXTURE_LOAD_PATH}/public")
94+
middleware.use ActionDispatch::DebugExceptions
95+
middleware.use ActionDispatch::Callbacks
96+
middleware.use ActionDispatch::Cookies
97+
middleware.use ActionDispatch::Flash
98+
middleware.use Rack::Head
12999
end
130100
end
131101

@@ -147,22 +117,12 @@ class TestCase
147117
include ActionDispatch::TestProcess
148118

149119
def self.with_routes(&block)
150-
routes = ActionDispatch::Routing::RouteSet.new
151-
routes.draw(&block)
152-
include Module.new {
153-
define_method(:setup) do
154-
super()
155-
@routes = routes
156-
@controller.singleton_class.include @routes.url_helpers if @controller
157-
end
158-
}
159-
routes
160-
end
120+
setup do
121+
@routes = ActionDispatch::Routing::RouteSet.new
122+
@routes.draw(&block)
161123

162-
def with_routes(&block)
163-
@routes = ActionDispatch::Routing::RouteSet.new
164-
@routes.draw(&block)
165-
@routes
124+
@controller.singleton_class.include @routes.url_helpers if @controller
125+
end
166126
end
167127
end
168128
end

actionview/test/actionpack/controller/view_paths_test.rb

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -139,14 +139,16 @@ def test_view_paths_override
139139

140140
def test_view_paths_override_for_layouts_in_controllers_with_a_module
141141
@controller = Test::SubController.new
142-
with_routes do
143-
get :hello_world, to: "view_load_paths_test/test/sub#hello_world"
144-
end
142+
with_routing do |routes|
143+
routes.draw do
144+
get :hello_world, to: "view_load_paths_test/test/sub#hello_world"
145+
end
145146

146-
Test::SubController.view_paths = [ "#{FIXTURE_LOAD_PATH}/override", FIXTURE_LOAD_PATH, "#{FIXTURE_LOAD_PATH}/override2" ]
147-
get :hello_world
148-
assert_response :success
149-
assert_equal "layout: Hello overridden world!", @response.body
147+
Test::SubController.view_paths = [ "#{FIXTURE_LOAD_PATH}/override", FIXTURE_LOAD_PATH, "#{FIXTURE_LOAD_PATH}/override2" ]
148+
get :hello_world
149+
assert_response :success
150+
assert_equal "layout: Hello overridden world!", @response.body
151+
end
150152
end
151153

152154
def test_view_paths_override_at_request_time

0 commit comments

Comments
 (0)