Skip to content

Commit 9854b4e

Browse files
committed
Fix 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 ac01b7c commit 9854b4e

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
@@ -60,6 +60,48 @@ def assertion_instance
6060
end
6161
end
6262

63+
# @private
64+
class ControllerAssertionDelegator < AssertionDelegator
65+
module Setup
66+
extend ActiveSupport::Concern
67+
68+
included { init_setup }
69+
70+
module ClassMethods
71+
attr_reader :setup_methods, :setup_blocks
72+
attr_accessor :controller_class
73+
74+
def init_setup
75+
@setup_methods ||= []
76+
@setup_blocks ||= []
77+
end
78+
79+
def setup(*methods, &block)
80+
@setup_methods += methods
81+
@setup_blocks << block if block
82+
end
83+
end
84+
85+
def initialize(*)
86+
super
87+
self.class.controller_class = described_class
88+
run_setup
89+
end
90+
91+
def run_setup
92+
self.class.setup_methods.each { |m| send(m) }
93+
self.class.setup_blocks.each { |b| b.call }
94+
end
95+
end
96+
97+
def initialize(*args)
98+
args << Setup
99+
args << ActionDispatch::Assertions::RoutingAssertions
100+
args << ActionController::TestCase::Behavior
101+
super(*args)
102+
end
103+
end
104+
63105
# Adapts example groups for `Minitest::Test::LifecycleHooks`
64106
#
65107
# @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+
ControllerAssertionDelegatorInstance = 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 ControllerAssertionDelegatorInstance
1917

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

0 commit comments

Comments
 (0)