Commit d2195bb 1 parent 951ea8f commit d2195bb Copy full SHA for d2195bb
File tree 2 files changed +54
-2
lines changed
actionpack/lib/action_dispatch/testing/assertions
railties/test/application
2 files changed +54
-2
lines changed Original file line number Diff line number Diff line change @@ -25,7 +25,7 @@ def with_routing(&block)
25
25
old_integration_session = nil
26
26
27
27
setup do
28
- old_routes = app . routes
28
+ old_routes = initialize_lazy_routes ( app . routes )
29
29
old_routes_call_method = old_routes . method ( :call )
30
30
old_integration_session = integration_session
31
31
create_routes ( &block )
@@ -38,7 +38,7 @@ def with_routing(&block)
38
38
end
39
39
40
40
def with_routing ( &block )
41
- old_routes = app . routes
41
+ old_routes = initialize_lazy_routes ( app . routes )
42
42
old_routes_call_method = old_routes . method ( :call )
43
43
old_integration_session = integration_session
44
44
create_routes ( &block )
@@ -47,6 +47,14 @@ def with_routing(&block)
47
47
end
48
48
49
49
private
50
+ def initialize_lazy_routes ( routes )
51
+ if defined? ( Rails ::Engine ::LazyRouteSet ) && routes . is_a? ( Rails ::Engine ::LazyRouteSet )
52
+ routes . tap ( &:routes )
53
+ else
54
+ routes
55
+ end
56
+ end
57
+
50
58
def create_routes
51
59
app = self . app
52
60
routes = ActionDispatch ::Routing ::RouteSet . new
Original file line number Diff line number Diff line change @@ -72,5 +72,49 @@ def test_app_returns_action_dispatch_test_app_by_default
72
72
output = rails ( "test" )
73
73
assert_match ( /0 failures, 0 errors/ , output )
74
74
end
75
+
76
+ test "routes are loaded before inserting test routes" do
77
+ app_file "config/routes.rb" , <<-RUBY
78
+ Rails.application.routes.draw do
79
+ get "/c", as: :c, to: "users#show"
80
+ end
81
+ RUBY
82
+
83
+ app_file "config/initializers/after_routes_loaded.rb" , <<-RUBY
84
+ Rails.configuration.after_routes_loaded do
85
+ $after_routes_loaded = true
86
+ end
87
+ RUBY
88
+
89
+ app_file "app/controllers/users_controller.rb" , <<-RUBY
90
+ class UsersController < ActionController::Base
91
+ def index
92
+ render json: { loaded: $after_routes_loaded }
93
+ end
94
+ end
95
+ RUBY
96
+
97
+ app_file "test/controllers/users_controller_test.rb" , <<-RUBY
98
+ require "test_helper"
99
+
100
+ class UsersControllerTest < ActionDispatch::IntegrationTest
101
+ with_routing do |set|
102
+ set.draw do
103
+ resources(:users, only: :index)
104
+ end
105
+ end
106
+
107
+ test "lazy route loading" do
108
+ get("/users")
109
+
110
+ assert_equal({ "loaded" => true }, JSON.parse(response.body))
111
+ end
112
+ end
113
+ RUBY
114
+
115
+ output = rails ( "test" )
116
+ assert_match ( /0 failures, 0 errors/ , output )
117
+ end
118
+
75
119
end
76
120
end
You can’t perform that action at this time.
0 commit comments