From fab4f02da3e98446e913f2f3ba499d94ec95b87e Mon Sep 17 00:00:00 2001 From: Elliot Winkler Date: Wed, 24 Dec 2014 16:44:38 -0500 Subject: [PATCH] Fix AC matcher tests for Rails 4.2 Modify helper methods provided by ControllerBuilder so that we use as much of rspec-rails's controller example group capabilities as possible. All of this can still be improved but this is a good start for now. --- .../action_controller/redirect_to_matcher.rb | 2 +- .../unit/helpers/controller_builder.rb | 15 ++--- .../redirect_to_matcher_spec.rb | 2 +- .../render_with_layout_matcher_spec.rb | 11 ++-- .../action_controller/route_matcher_spec.rb | 55 ++++++++++--------- .../strong_parameters_matcher_spec.rb | 2 +- spec/unit_spec_helper.rb | 4 ++ 7 files changed, 46 insertions(+), 45 deletions(-) diff --git a/lib/shoulda/matchers/action_controller/redirect_to_matcher.rb b/lib/shoulda/matchers/action_controller/redirect_to_matcher.rb index 1637a41e..3fd4184f 100644 --- a/lib/shoulda/matchers/action_controller/redirect_to_matcher.rb +++ b/lib/shoulda/matchers/action_controller/redirect_to_matcher.rb @@ -72,7 +72,7 @@ module Shoulda end def description - "redirect to #{@location}" + "redirect to #{@location.inspect}" end private diff --git a/spec/support/unit/helpers/controller_builder.rb b/spec/support/unit/helpers/controller_builder.rb index 1bd62a23..50b033f2 100644 --- a/spec/support/unit/helpers/controller_builder.rb +++ b/spec/support/unit/helpers/controller_builder.rb @@ -16,10 +16,7 @@ module UnitTests end def define_routes(&block) - @routes = $test_app.draw_routes(&block) - class << self - include ActionDispatch::Assertions - end + self.routes = $test_app.draw_routes(&block) end def build_fake_response(opts = {}, &block) @@ -40,19 +37,15 @@ module UnitTests create_view("examples/#{partial}.html.erb", 'partial') setup_rails_controller_test(controller_class) + self.class.render_views(true) + get action - @controller + controller end def setup_rails_controller_test(controller_class) @controller = controller_class.new - @request = ::ActionController::TestRequest.new - @response = ::ActionController::TestResponse.new - - class << self - include ActionController::TestCase::Behavior - end end def create_view(path, contents) diff --git a/spec/unit/shoulda/matchers/action_controller/redirect_to_matcher_spec.rb b/spec/unit/shoulda/matchers/action_controller/redirect_to_matcher_spec.rb index 64af68c1..31311043 100644 --- a/spec/unit/shoulda/matchers/action_controller/redirect_to_matcher_spec.rb +++ b/spec/unit/shoulda/matchers/action_controller/redirect_to_matcher_spec.rb @@ -37,6 +37,6 @@ describe Shoulda::Matchers::ActionController::RedirectToMatcher, type: :controll it 'provides the correct description when provided a block' do matcher = redirect_to('somewhere else') { '/some/other/url' } - expect(matcher.description).to eq 'redirect to somewhere else' + expect(matcher.description).to eq 'redirect to "somewhere else"' end end diff --git a/spec/unit/shoulda/matchers/action_controller/render_with_layout_matcher_spec.rb b/spec/unit/shoulda/matchers/action_controller/render_with_layout_matcher_spec.rb index a9b484a7..2bf4a4b1 100644 --- a/spec/unit/shoulda/matchers/action_controller/render_with_layout_matcher_spec.rb +++ b/spec/unit/shoulda/matchers/action_controller/render_with_layout_matcher_spec.rb @@ -41,15 +41,18 @@ describe Shoulda::Matchers::ActionController::RenderWithLayoutMatcher, type: :co context 'given a context with layouts' do it 'accepts that layout in that context' do - set_in_context_layout('happy') + context = Object.new + set_layout_in_context(context, 'happy') - expect(controller_without_layout).to render_with_layout('happy').in_context(self) + expect(controller_without_layout). + to render_with_layout('happy'). + in_context(context) end - def set_in_context_layout(layout) + def set_layout_in_context(context, layout) layouts = Hash.new(0) layouts[layout] = 1 - self.instance_variable_set(layouts_ivar, layouts) + context.instance_variable_set(layouts_ivar, layouts) end def layouts_ivar diff --git a/spec/unit/shoulda/matchers/action_controller/route_matcher_spec.rb b/spec/unit/shoulda/matchers/action_controller/route_matcher_spec.rb index c5661a2e..980f770f 100644 --- a/spec/unit/shoulda/matchers/action_controller/route_matcher_spec.rb +++ b/spec/unit/shoulda/matchers/action_controller/route_matcher_spec.rb @@ -5,19 +5,19 @@ describe 'Shoulda::Matchers::ActionController::RouteMatcher', type: :controller context 'when controller and action are specified as explicit options' do it 'accepts' do expect(controller_with_defined_routes). - to route(:get, "/#{controller_name}"). + to route(:get, "/#{controller_path}"). to(action: 'index') end it 'accepts a symbol controller' do expect(controller_with_defined_routes). - to route(:get, "/#{controller_name}"). - to(controller: controller_name.to_sym, action: 'index') + to route(:get, "/#{controller_path}"). + to(controller: controller_path.to_sym, action: 'index') end it 'accepts a symbol action' do expect(controller_with_defined_routes). - to route(:get, "/#{controller_name}"). + to route(:get, "/#{controller_path}"). to(action: :index) end @@ -31,20 +31,20 @@ describe 'Shoulda::Matchers::ActionController::RouteMatcher', type: :controller define_controller_with_defined_routes other_controller = define_controller('Other').new expect(other_controller). - not_to route(:get, "/#{controller_name}"). + not_to route(:get, "/#{controller_path}"). to(action: 'index') end context 'when route has parameters' do it 'accepts a non-string parameter' do expect(controller_with_defined_routes). - to route(:get, "/#{controller_name}/1"). + to route(:get, "/#{controller_path}/1"). to(action: 'show', id: 1) end it 'rejects a route for different parameters' do expect(controller_with_defined_routes). - not_to route(:get, "/#{controller_name}/1"). + not_to route(:get, "/#{controller_path}/1"). to(action: 'show', some: 'other', params: 'here') end end @@ -53,42 +53,50 @@ describe 'Shoulda::Matchers::ActionController::RouteMatcher', type: :controller context 'when controller and action are specified as a joined string' do it 'accepts' do expect(controller_with_defined_routes). - to route(:get, "/#{controller_name}"). - to("#{controller_name}#index") + to route(:get, "/#{controller_path}"). + to("#{controller_path}#index") end context 'when route has parameters' do it 'accepts a non-string parameter' do expect(controller_with_defined_routes). - to route(:get, "/#{controller_name}/1"). - to("#{controller_name}#show", id: 1) + to route(:get, "/#{controller_path}/1"). + to("#{controller_path}#show", id: 1) end end end def controller_with_defined_routes @_controller_with_defined_routes ||= begin - _controller_name = controller_name + controller_class = define_controller(controller_name) + _controller_path = controller_path + + setup_rails_controller_test(controller_class) define_routes do - get "/#{_controller_name}", to: "#{_controller_name}#index" - get "/#{_controller_name}/:id", to: "#{_controller_name}#show" + get "/#{_controller_path}", to: "#{_controller_path}#index" + get "/#{_controller_path}/:id", to: "#{_controller_path}#show" end controller end end + def controller_path + controller_name.sub(/Controller$/, '').underscore + end + alias_method :define_controller_with_defined_routes, :controller_with_defined_routes end context 'given a controller with a defined glob url' do it 'accepts glob route' do - controller = define_controller('Examples').new + controller_class = define_controller('Examples') + setup_rails_controller_test(controller_class) define_routes do - get 'examples/*id', to: 'examples#example' + get '/examples/*id', to: 'examples#example' end expect(controller).to route(:get, '/examples/foo/bar'). @@ -98,27 +106,20 @@ describe 'Shoulda::Matchers::ActionController::RouteMatcher', type: :controller context 'given a controller that is not namespaced' do it_behaves_like 'a controller with a defined route' do - def controller - define_controller(controller_name).new - end - def controller_name - 'examples' + 'ExamplesController' end end end context 'given a controller that is namespaced' do it_behaves_like 'a controller with a defined route' do - def controller - @_controller ||= begin - define_module('Admin') - define_controller('Admin::Examples').new - end + before do + define_module('Admin') end def controller_name - 'admin/examples' + 'Admin::ExamplesController' end end end diff --git a/spec/unit/shoulda/matchers/action_controller/strong_parameters_matcher_spec.rb b/spec/unit/shoulda/matchers/action_controller/strong_parameters_matcher_spec.rb index 984ab7e6..b9a9dbd8 100644 --- a/spec/unit/shoulda/matchers/action_controller/strong_parameters_matcher_spec.rb +++ b/spec/unit/shoulda/matchers/action_controller/strong_parameters_matcher_spec.rb @@ -37,7 +37,7 @@ describe Shoulda::Matchers::ActionController, type: :controller do end end -describe Shoulda::Matchers::ActionController::StrongParametersMatcher do +describe Shoulda::Matchers::ActionController::StrongParametersMatcher, type: :controller do describe '#description' do it 'returns the correct string' do options = { action: :create, method: :post } diff --git a/spec/unit_spec_helper.rb b/spec/unit_spec_helper.rb index 66546766..5fc11c8e 100644 --- a/spec/unit_spec_helper.rb +++ b/spec/unit_spec_helper.rb @@ -45,6 +45,10 @@ RSpec.configure do |config| config.infer_spec_type_from_file_location! end + config.before(:all, type: :controller) do + self.class.controller(ApplicationController) { } + end + UnitTests::ActiveModelHelpers.configure_example_group(config) UnitTests::ActiveModelVersions.configure_example_group(config) UnitTests::ActiveResourceBuilder.configure_example_group(config)