Skip to content

Commit

Permalink
Merge pull request #50727 from seanpdoyle/action-view-tests-simplify-…
Browse files Browse the repository at this point in the history
…routing

Action View Tests: Use `#with_routing` helper
  • Loading branch information
rafaelfranca committed Apr 19, 2024
2 parents d60a234 + 15ddb5a commit ea2bb23
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 59 deletions.
64 changes: 12 additions & 52 deletions actionview/test/abstract_unit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,19 +72,6 @@ def render_erb(string)
end
end

class RoutedRackApp
attr_reader :routes

def initialize(routes, &blk)
@routes = routes
@stack = ActionDispatch::MiddlewareStack.new(&blk).build(@routes)
end

def call(env)
@stack.call(env)
end
end

class BasicController
attr_accessor :request, :response

Expand All @@ -102,30 +89,13 @@ def config
end

class ActionDispatch::IntegrationTest < ActiveSupport::TestCase
def self.build_app(routes = nil)
routes ||= ActionDispatch::Routing::RouteSet.new.tap { |rs|
rs.draw { }
}
RoutedRackApp.new(routes) do |middleware|
middleware.use ActionDispatch::ShowExceptions, ActionDispatch::PublicExceptions.new("#{FIXTURE_LOAD_PATH}/public")
middleware.use ActionDispatch::DebugExceptions
middleware.use ActionDispatch::Callbacks
middleware.use ActionDispatch::Cookies
middleware.use ActionDispatch::Flash
middleware.use Rack::Head
yield(middleware) if block_given?
end
end

self.app = build_app

def with_routing(&block)
temporary_routes = ActionDispatch::Routing::RouteSet.new
old_app, self.class.app = self.class.app, self.class.build_app(temporary_routes)

yield temporary_routes
ensure
self.class.app = old_app
self.app = ActionDispatch::MiddlewareStack.new do |middleware|
middleware.use ActionDispatch::ShowExceptions, ActionDispatch::PublicExceptions.new("#{FIXTURE_LOAD_PATH}/public")
middleware.use ActionDispatch::DebugExceptions
middleware.use ActionDispatch::Callbacks
middleware.use ActionDispatch::Cookies
middleware.use ActionDispatch::Flash
middleware.use Rack::Head
end
end

Expand All @@ -147,22 +117,12 @@ class TestCase
include ActionDispatch::TestProcess

def self.with_routes(&block)
routes = ActionDispatch::Routing::RouteSet.new
routes.draw(&block)
include Module.new {
define_method(:setup) do
super()
@routes = routes
@controller.singleton_class.include @routes.url_helpers if @controller
end
}
routes
end
setup do
@routes = ActionDispatch::Routing::RouteSet.new
@routes.draw(&block)

def with_routes(&block)
@routes = ActionDispatch::Routing::RouteSet.new
@routes.draw(&block)
@routes
@controller.singleton_class.include @routes.url_helpers if @controller
end
end
end
end
Expand Down
16 changes: 9 additions & 7 deletions actionview/test/actionpack/controller/view_paths_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -139,14 +139,16 @@ def test_view_paths_override

def test_view_paths_override_for_layouts_in_controllers_with_a_module
@controller = Test::SubController.new
with_routes do
get :hello_world, to: "view_load_paths_test/test/sub#hello_world"
end
with_routing do |routes|
routes.draw do
get :hello_world, to: "view_load_paths_test/test/sub#hello_world"
end

Test::SubController.view_paths = [ "#{FIXTURE_LOAD_PATH}/override", FIXTURE_LOAD_PATH, "#{FIXTURE_LOAD_PATH}/override2" ]
get :hello_world
assert_response :success
assert_equal "layout: Hello overridden world!", @response.body
Test::SubController.view_paths = [ "#{FIXTURE_LOAD_PATH}/override", FIXTURE_LOAD_PATH, "#{FIXTURE_LOAD_PATH}/override2" ]
get :hello_world
assert_response :success
assert_equal "layout: Hello overridden world!", @response.body
end
end

def test_view_paths_override_at_request_time
Expand Down

0 comments on commit ea2bb23

Please sign in to comment.