Skip to content

Commit ae5c8be

Browse files
committed
with_routing not working with get :index
As guys previously mentioned in the thread the problem is that `#get` is called on other context then `#with_routing`. It is caused by https://github.com/rspec/rspec-rails/blob/master/lib/rspec/rails/example/controller_example_group.rb#L5. ActionDispatch::Assertions::RoutingAssertions is adding #with_routing to an assertion_instance, and ActionController::TestCase::Behavior is adding #get to ControllerExampleGroup. So I decided to add an ControllerAssertionDelegator which will include both of them. Actually AssertionDelegator missed some methods, so I featured them in that delegator. Closes #1652
1 parent 0e83a47 commit ae5c8be

File tree

2 files changed

+44
-4
lines changed

2 files changed

+44
-4
lines changed

lib/rspec/rails/adapters.rb

+42
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,48 @@ def assertion_instance
112112
end
113113
end
114114

115+
# @private
116+
class ControllerAssertionDelegator < AssertionDelegator
117+
module Setup
118+
extend ActiveSupport::Concern
119+
120+
included { init_setup }
121+
122+
module ClassMethods
123+
attr_reader :setup_methods, :setup_blocks
124+
attr_accessor :controller_class
125+
126+
def init_setup
127+
@setup_methods ||= []
128+
@setup_blocks ||= []
129+
end
130+
131+
def setup(*methods, &block)
132+
@setup_methods += methods
133+
@setup_blocks << block if block
134+
end
135+
end
136+
137+
def initialize(*)
138+
super
139+
self.class.controller_class = described_class
140+
run_setup
141+
end
142+
143+
def run_setup
144+
self.class.setup_methods.each { |m| send(m) }
145+
self.class.setup_blocks.each { |b| b.call }
146+
end
147+
end
148+
149+
def initialize(*args)
150+
args << Setup
151+
args << ActionDispatch::Assertions::RoutingAssertions
152+
args << ActionController::TestCase::Behavior
153+
super(*args)
154+
end
155+
end
156+
115157
# Adapts example groups for `Minitest::Test::LifecycleHooks`
116158
#
117159
# @private

lib/rspec/rails/example/controller_example_group.rb

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
module RSpec
22
module Rails
33
# @private
4-
ControllerAssertionDelegator = RSpec::Rails::AssertionDelegator.new(
5-
ActionDispatch::Assertions::RoutingAssertions
6-
)
4+
ControllerAssertionDelegatorConst = RSpec::Rails::ControllerAssertionDelegator.new
75

86
# @api public
97
# Container module for controller spec functionality.
@@ -15,7 +13,7 @@ module ControllerExampleGroup
1513
include RSpec::Rails::Matchers::RedirectTo
1614
include RSpec::Rails::Matchers::RenderTemplate
1715
include RSpec::Rails::Matchers::RoutingMatchers
18-
include ControllerAssertionDelegator
16+
include ControllerAssertionDelegatorConst
1917

2018
# Class-level DSL for controller specs.
2119
module ClassMethods

0 commit comments

Comments
 (0)