Skip to content

Commit

Permalink
Fix controller example group specs to be less stub-happy.
Browse files Browse the repository at this point in the history
The fact that they stub `controller_class` (which is
changed by the `controller` macro being tested) got
in the way of further changes I need to make and was
a bad idea to begin with.
  • Loading branch information
myronmarston committed Apr 23, 2014
1 parent b441f7f commit 919c97a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 20 deletions.
1 change: 1 addition & 0 deletions lib/rspec/rails/example/controller_example_group.rb
Expand Up @@ -59,6 +59,7 @@ def controller(base_class = nil, &body)
base_class ||= RSpec.configuration.infer_base_class_for_anonymous_controllers? ?
controller_class :
root_controller
base_class ||= root_controller

metadata[:example_group][:described_class] = Class.new(base_class) do
def self.name; "AnonymousController"; end
Expand Down
33 changes: 14 additions & 19 deletions spec/rspec/rails/example/controller_example_group_spec.rb
Expand Up @@ -8,12 +8,14 @@ module RSpec::Rails
it { is_expected.to be_included_in_files_in('./spec/controllers/') }
it { is_expected.to be_included_in_files_in('.\\spec\\controllers\\') }

let(:group) do
RSpec::Core::ExampleGroup.describe do
def group_for(klass)
RSpec::Core::ExampleGroup.describe klass do
include ControllerExampleGroup
end
end

let(:group) { group_for ApplicationController }

it "includes routing matchers" do
expect(group.included_modules).to include(RSpec::Rails::Matchers::RoutingMatchers)
end
Expand Down Expand Up @@ -54,6 +56,7 @@ module RSpec::Rails
allow(example).to receive(:controller).and_return(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
Expand All @@ -76,49 +79,41 @@ module RSpec::Rails
end

describe "with inferred anonymous controller" do
before do
allow(group).to receive(:controller_class).and_return(Class.new)
end
around { |ex| with_isolated_config(&ex) }
let(:anonymous_klass) { Class.new }
let(:group) { group_for(anonymous_klass) }

context "when infer_base_class_for_anonymous_controllers is true" do
before do
allow(RSpec.configuration).to receive(:infer_base_class_for_anonymous_controllers?).and_return(true)
RSpec.configuration.infer_base_class_for_anonymous_controllers = true
end

it "infers the anonymous controller class" do
group.controller { }

controller_class = group.metadata[:example_group][:described_class]
expect(controller_class.superclass).to eq(group.controller_class)
expect(group.controller_class.superclass).to eq(anonymous_klass)
end

it "infers the anonymous controller class when no ApplicationController is present" do
hide_const '::ApplicationController'
group.controller { }

controller_class = group.metadata[:example_group][:described_class]
expect(controller_class.superclass).to eq(group.controller_class)
expect(group.controller_class.superclass).to eq(anonymous_klass)
end
end

context "when infer_base_class_for_anonymous_controllers is false" do
before do
allow(RSpec.configuration).to receive(:infer_base_class_for_anonymous_controllers?).and_return(false)
RSpec.configuration.infer_base_class_for_anonymous_controllers = false
end

it "sets the anonymous controller class to ApplicationController" do
group.controller { }

controller_class = group.metadata[:example_group][:described_class]
expect(controller_class.superclass).to eq(ApplicationController)
expect(group.controller_class.superclass).to eq(ApplicationController)
end

it "sets the anonymous controller class to ActiveController::Base when no ApplicationController is present" do
hide_const '::ApplicationController'
group.controller { }

controller_class = group.metadata[:example_group][:described_class]
expect(controller_class.superclass).to eq(ActionController::Base)
expect(group.controller_class.superclass).to eq(ActionController::Base)
end
end
end
Expand Down
3 changes: 2 additions & 1 deletion spec/support/helpers.rb
Expand Up @@ -25,8 +25,9 @@ def with_isolated_config
c.add_setting :use_instantiated_fixtures
c.add_setting :global_fixtures
c.add_setting :fixture_path
c.add_setting :infer_base_class_for_anonymous_controllers, :default => false
end
yield
yield RSpec.configuration
RSpec.configuration = original_config
end

Expand Down

0 comments on commit 919c97a

Please sign in to comment.