Skip to content

Commit

Permalink
with_routing not working with get :index
Browse files Browse the repository at this point in the history
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 rspec#1652
  • Loading branch information
sclinede committed Jan 20, 2017
1 parent 0e83a47 commit 641d3e0
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
41 changes: 41 additions & 0 deletions lib/rspec/rails/adapters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ def self.disable_testunit_autorun

# @private
class AssertionDelegator < Module

def initialize(*assertion_modules)
assertion_class = Class.new(SimpleDelegator) do
include ::RSpec::Rails::Assertions
Expand Down Expand Up @@ -112,6 +113,46 @@ def assertion_instance
end
end

class ControllerAssertionDelegator < AssertionDelegator
module Setup
extend ActiveSupport::Concern

included { init_setup }

module ClassMethods
attr_reader :setup_methods, :setup_blocks
attr_accessor :controller_class

def init_setup
@setup_methods ||= []
@setup_blocks ||= []
end

def setup(*methods, &block)
@setup_methods += methods
@setup_blocks << block if block
end
end

def initialize(*)
super
self.class.controller_class = described_class
run_setup
end

def run_setup
self.class.setup_methods.each { |m| send(m) }
self.class.setup_blocks.each { |b| b.call }
end
end

def initialize(*args)
args << Setup
args << ActionController::TestCase::Behavior
super(*args)
end
end

# Adapts example groups for `Minitest::Test::LifecycleHooks`
#
# @private
Expand Down
2 changes: 1 addition & 1 deletion lib/rspec/rails/example/controller_example_group.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module RSpec
module Rails
# @private
ControllerAssertionDelegator = RSpec::Rails::AssertionDelegator.new(
ControllerAssertionDelegator = RSpec::Rails::ControllerAssertionDelegator.new(
ActionDispatch::Assertions::RoutingAssertions
)

Expand Down

0 comments on commit 641d3e0

Please sign in to comment.