Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix with_routing not working with get :index #1777

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions lib/rspec/rails/adapters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,48 @@ def assertion_instance
end
end

# @private
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You may have found it yourself already, but anyway - if you add a def teardown(*); end here, the spec starts failing with a different error.

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
Expand Down
6 changes: 2 additions & 4 deletions lib/rspec/rails/example/controller_example_group.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
module RSpec
module Rails
# @private
ControllerAssertionDelegator = RSpec::Rails::AssertionDelegator.new(
ActionDispatch::Assertions::RoutingAssertions
)
ControllerAssertionDelegatorInstance = RSpec::Rails::ControllerAssertionDelegator.new

# @api public
# Container module for controller spec functionality.
Expand All @@ -15,7 +13,7 @@ module ControllerExampleGroup
include RSpec::Rails::Matchers::RedirectTo
include RSpec::Rails::Matchers::RenderTemplate
include RSpec::Rails::Matchers::RoutingMatchers
include ControllerAssertionDelegator
include ControllerAssertionDelegatorInstance

# Class-level DSL for controller specs.
module ClassMethods
Expand Down
29 changes: 29 additions & 0 deletions spec/rspec/rails/example/controller_example_group_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,35 @@ def my_helper

example.foos_url
end

context "when controller is not a stub" do
pirj marked this conversation as resolved.
Show resolved Hide resolved
let(:custom_contoller) do
stub_const(
"CustomController",
Class.new(ActionController::Base) do
def index
render plain: "ok"
end
end
)
end

let(:group) { group_for custom_contoller }
let(:controller) { custom_contoller }

it "properly delegates routing assertions" do
with_isolated_stderr do
example.with_routing do |map|
map.draw do
get "custom" => "custom#index"
end

expect { example.get :index }.not_to raise_error
end
end
end
end

end

describe "#bypass_rescue" do
Expand Down