Skip to content

Commit e5daafb

Browse files
authored
Merge pull request rails#48407 from andrewn617/create_class_level_with_routing_helper
Create a class level #with_routing helper
2 parents aff1db4 + 37f3ce9 commit e5daafb

File tree

4 files changed

+134
-26
lines changed

4 files changed

+134
-26
lines changed

actionpack/CHANGELOG.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,27 @@
1+
* The `with_routing` helper can now be called at the class level. When called at the class level, the routes will
2+
be setup before each test, and reset after every test. For example:
3+
4+
```ruby
5+
class RoutingTest < ActionController::TestCase
6+
with_routing do |routes|
7+
routes.draw do
8+
resources :articles
9+
resources :authors
10+
end
11+
end
12+
13+
def test_articles_route
14+
assert_routing("/articles", controller: "articles", action: "index")
15+
end
16+
17+
def test_authors_route
18+
assert_routing("/authors", controller: "authors", action: "index")
19+
end
20+
end
21+
```
22+
23+
*Andrew Novoselac*
24+
125
* The `Mime::Type` now supports handling types with parameters and correctly handles quotes.
226
When parsing the accept header, the parameters before the q-parameter are kept and if a matching mime-type exists it is used.
327
To keep the current functionality, a fallback is created to look for the media-type without the parameters.

actionpack/lib/action_dispatch/testing/assertions.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
module ActionDispatch
88
module Assertions
9+
extend ActiveSupport::Concern
10+
911
include ResponseAssertions
1012
include RoutingAssertions
1113
include Rails::Dom::Testing::Assertions

actionpack/lib/action_dispatch/testing/assertions/routing.rb

Lines changed: 66 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,37 @@ module ActionDispatch
99
module Assertions
1010
# Suite of assertions to test routes generated by \Rails and the handling of requests made to them.
1111
module RoutingAssertions
12+
extend ActiveSupport::Concern
13+
14+
class_methods do
15+
# A helper to make it easier to test different route configurations.
16+
# This method temporarily replaces @routes with a new RouteSet instance
17+
# before each test.
18+
#
19+
# The new instance is yielded to the passed block. Typically the block
20+
# will create some routes using <tt>set.draw { match ... }</tt>:
21+
#
22+
# with_routing do |set|
23+
# set.draw do
24+
# resources :users
25+
# end
26+
# assert_equal "/users", users_path
27+
# end
28+
#
29+
def with_routing(&block)
30+
old_routes, old_controller = nil
31+
32+
setup do
33+
old_routes, old_controller = @routes, @controller
34+
create_routes(&block)
35+
end
36+
37+
teardown do
38+
reset_routes(old_routes, old_controller)
39+
end
40+
end
41+
end
42+
1243
def setup # :nodoc:
1344
@routes ||= nil
1445
super
@@ -150,33 +181,11 @@ def assert_routing(path, options, defaults = {}, extras = {}, message = nil)
150181
# assert_equal "/users", users_path
151182
# end
152183
#
153-
def with_routing
154-
old_routes, @routes = @routes, ActionDispatch::Routing::RouteSet.new
155-
if defined?(@controller) && @controller
156-
old_controller, @controller = @controller, @controller.clone
157-
_routes = @routes
158-
159-
@controller.singleton_class.include(_routes.url_helpers)
160-
161-
if @controller.respond_to? :view_context_class
162-
view_context_class = Class.new(@controller.view_context_class) do
163-
include _routes.url_helpers
164-
end
165-
166-
custom_view_context = Module.new {
167-
define_method(:view_context_class) do
168-
view_context_class
169-
end
170-
}
171-
@controller.extend(custom_view_context)
172-
end
173-
end
174-
yield @routes
184+
def with_routing(&block)
185+
old_routes, old_controller = @routes, @controller
186+
create_routes(&block)
175187
ensure
176-
@routes = old_routes
177-
if defined?(@controller) && @controller
178-
@controller = old_controller
179-
end
188+
reset_routes(old_routes, old_controller)
180189
end
181190

182191
# ROUTES TODO: These assertions should really work in an integration context
@@ -190,6 +199,37 @@ def method_missing(selector, *args, &block)
190199
ruby2_keywords(:method_missing)
191200

192201
private
202+
def create_routes
203+
@routes = ActionDispatch::Routing::RouteSet.new
204+
if defined?(@controller) && @controller
205+
@controller = @controller.clone
206+
_routes = @routes
207+
208+
@controller.singleton_class.include(_routes.url_helpers)
209+
210+
if @controller.respond_to? :view_context_class
211+
view_context_class = Class.new(@controller.view_context_class) do
212+
include _routes.url_helpers
213+
end
214+
215+
custom_view_context = Module.new {
216+
define_method(:view_context_class) do
217+
view_context_class
218+
end
219+
}
220+
@controller.extend(custom_view_context)
221+
end
222+
end
223+
yield @routes
224+
end
225+
226+
def reset_routes(old_routes, old_controller)
227+
@routes = old_routes
228+
if defined?(@controller) && @controller
229+
@controller = old_controller
230+
end
231+
end
232+
193233
# Recognizes the route for a given path.
194234
def recognized_request_for(path, extras = {}, msg)
195235
if path.is_a?(Hash)

actionpack/test/dispatch/routing_assertions_test.rb

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,4 +223,46 @@ def test_with_routing
223223
end
224224
end
225225
end
226+
227+
class WithRoutingTest < ActionController::TestCase
228+
def before_setup
229+
@routes = ActionDispatch::Routing::RouteSet.new
230+
@routes.draw do
231+
resources :articles
232+
end
233+
234+
super
235+
end
236+
237+
with_routing do |routes|
238+
routes.draw do
239+
resources :articles, path: "artikel"
240+
end
241+
end
242+
243+
def test_with_routing_for_the_entire_test_file
244+
assert_routing("/artikel", controller: "articles", action: "index")
245+
assert_raise(Assertion) do
246+
assert_routing("/articles", controller: "articles", action: "index")
247+
end
248+
end
249+
250+
def test_with_routing_for_entire_test_file_can_be_overwritten_for_individual_test
251+
with_routing do |routes|
252+
routes.draw do
253+
resources :articles, path: "articolo"
254+
end
255+
256+
assert_routing("/articolo", controller: "articles", action: "index")
257+
assert_raise(Assertion) do
258+
assert_routing("/artikel", controller: "articles", action: "index")
259+
end
260+
end
261+
262+
assert_routing("/artikel", controller: "articles", action: "index")
263+
assert_raise(Assertion) do
264+
assert_routing("/articolo", controller: "articles", action: "index")
265+
end
266+
end
267+
end
226268
end

0 commit comments

Comments
 (0)