diff --git a/lib/rspec/rails/adapters.rb b/lib/rspec/rails/adapters.rb index 63b2ed4f5c..9e187f9597 100644 --- a/lib/rspec/rails/adapters.rb +++ b/lib/rspec/rails/adapters.rb @@ -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 @@ -112,6 +113,47 @@ 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 << ActionDispatch::Assertions::RoutingAssertions + args << ActionController::TestCase::Behavior + super(*args) + end + end + # Adapts example groups for `Minitest::Test::LifecycleHooks` # # @private diff --git a/lib/rspec/rails/example/controller_example_group.rb b/lib/rspec/rails/example/controller_example_group.rb index 198f22ab5f..b11616d7e8 100644 --- a/lib/rspec/rails/example/controller_example_group.rb +++ b/lib/rspec/rails/example/controller_example_group.rb @@ -1,9 +1,7 @@ module RSpec module Rails # @private - ControllerAssertionDelegator = RSpec::Rails::AssertionDelegator.new( - ActionDispatch::Assertions::RoutingAssertions - ) + ControllerAssertionDelegatorConst = RSpec::Rails::ControllerAssertionDelegator.new # @api public # Container module for controller spec functionality. @@ -15,7 +13,7 @@ module ControllerExampleGroup include RSpec::Rails::Matchers::RedirectTo include RSpec::Rails::Matchers::RenderTemplate include RSpec::Rails::Matchers::RoutingMatchers - include ControllerAssertionDelegator + include ControllerAssertionDelegatorConst # Class-level DSL for controller specs. module ClassMethods