Skip to content
Closed
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ bin
Gemfile-custom
.rbenv-version
.rbx
.ruby-version
.ruby-gemset
5 changes: 2 additions & 3 deletions lib/rspec/rails/example/controller_example_group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,8 @@ def self.name
end
metadata[:example_group][:described_class].class_eval(&body)

orig_routes = nil
before do
orig_routes = self.routes
@orig_routes = self.routes
resource_name = @controller.respond_to?(:controller_name) ?
@controller.controller_name.to_sym : :anonymous
self.routes = ActionDispatch::Routing::RouteSet.new.tap { |r|
Expand All @@ -79,7 +78,7 @@ def self.name
end

after do
self.routes = orig_routes
self.routes = @orig_routes
@orig_routes = nil
end
end
Expand Down
24 changes: 19 additions & 5 deletions spec/rspec/rails/example/controller_example_group_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,24 +41,38 @@ module RSpec::Rails

describe "#controller" do
before do
orig_before_each_hooks = group.hooks[:before][:each].send(:hooks).dup
group.class_eval do
controller(Class.new) { }
end
@anonymous_controller_before_each_hook = (group.hooks[:before][:each].send(:hooks) - orig_before_each_hooks).first
end

it "delegates named route helpers to the underlying controller" do
it "delegates anonymous route helpers to the controller" do
controller = double('controller')
allow(controller).to receive(:anonymous_url).and_return('http://test.host/anonymous')

example = group.new
allow(example).to receive_messages(:controller => controller)

@anonymous_controller_before_each_hook.run(example)

expect(example.anonymous_url).to eq('http://test.host/anonymous')
end


it "delegates application route helpers to the controller" do
controller = double('controller')
allow(controller).to receive(:foos_url).and_return('http://test.host/foos')

example = group.new
allow(example).to receive_messages(:controller => controller)

# As in the routing example spec, this is pretty invasive, but not sure
# how to do it any other way as the correct operation relies on before
# hooks
routes = ActionDispatch::Routing::RouteSet.new
routes.draw { resources :foos }
example.instance_variable_set(:@orig_routes, routes)
example.instance_variable_set(:@routes, routes)

@anonymous_controller_before_each_hook.run(example)

expect(example.foos_url).to eq('http://test.host/foos')
end
Expand Down