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.
This commit is contained in:
Elliot Winkler 2014-12-24 16:44:38 -05:00
parent a9ab35117b
commit fab4f02da3
7 changed files with 46 additions and 45 deletions

View File

@ -72,7 +72,7 @@ module Shoulda
end
def description
"redirect to #{@location}"
"redirect to #{@location.inspect}"
end
private

View File

@ -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)

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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 }

View File

@ -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)