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:
parent
a9ab35117b
commit
fab4f02da3
|
@ -72,7 +72,7 @@ module Shoulda
|
||||||
end
|
end
|
||||||
|
|
||||||
def description
|
def description
|
||||||
"redirect to #{@location}"
|
"redirect to #{@location.inspect}"
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
|
@ -16,10 +16,7 @@ module UnitTests
|
||||||
end
|
end
|
||||||
|
|
||||||
def define_routes(&block)
|
def define_routes(&block)
|
||||||
@routes = $test_app.draw_routes(&block)
|
self.routes = $test_app.draw_routes(&block)
|
||||||
class << self
|
|
||||||
include ActionDispatch::Assertions
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def build_fake_response(opts = {}, &block)
|
def build_fake_response(opts = {}, &block)
|
||||||
|
@ -40,19 +37,15 @@ module UnitTests
|
||||||
create_view("examples/#{partial}.html.erb", 'partial')
|
create_view("examples/#{partial}.html.erb", 'partial')
|
||||||
|
|
||||||
setup_rails_controller_test(controller_class)
|
setup_rails_controller_test(controller_class)
|
||||||
|
self.class.render_views(true)
|
||||||
|
|
||||||
get action
|
get action
|
||||||
|
|
||||||
@controller
|
controller
|
||||||
end
|
end
|
||||||
|
|
||||||
def setup_rails_controller_test(controller_class)
|
def setup_rails_controller_test(controller_class)
|
||||||
@controller = controller_class.new
|
@controller = controller_class.new
|
||||||
@request = ::ActionController::TestRequest.new
|
|
||||||
@response = ::ActionController::TestResponse.new
|
|
||||||
|
|
||||||
class << self
|
|
||||||
include ActionController::TestCase::Behavior
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def create_view(path, contents)
|
def create_view(path, contents)
|
||||||
|
|
|
@ -37,6 +37,6 @@ describe Shoulda::Matchers::ActionController::RedirectToMatcher, type: :controll
|
||||||
it 'provides the correct description when provided a block' do
|
it 'provides the correct description when provided a block' do
|
||||||
matcher = redirect_to('somewhere else') { '/some/other/url' }
|
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
|
||||||
end
|
end
|
||||||
|
|
|
@ -41,15 +41,18 @@ describe Shoulda::Matchers::ActionController::RenderWithLayoutMatcher, type: :co
|
||||||
|
|
||||||
context 'given a context with layouts' do
|
context 'given a context with layouts' do
|
||||||
it 'accepts that layout in that context' 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
|
end
|
||||||
|
|
||||||
def set_in_context_layout(layout)
|
def set_layout_in_context(context, layout)
|
||||||
layouts = Hash.new(0)
|
layouts = Hash.new(0)
|
||||||
layouts[layout] = 1
|
layouts[layout] = 1
|
||||||
self.instance_variable_set(layouts_ivar, layouts)
|
context.instance_variable_set(layouts_ivar, layouts)
|
||||||
end
|
end
|
||||||
|
|
||||||
def layouts_ivar
|
def layouts_ivar
|
||||||
|
|
|
@ -5,19 +5,19 @@ describe 'Shoulda::Matchers::ActionController::RouteMatcher', type: :controller
|
||||||
context 'when controller and action are specified as explicit options' do
|
context 'when controller and action are specified as explicit options' do
|
||||||
it 'accepts' do
|
it 'accepts' do
|
||||||
expect(controller_with_defined_routes).
|
expect(controller_with_defined_routes).
|
||||||
to route(:get, "/#{controller_name}").
|
to route(:get, "/#{controller_path}").
|
||||||
to(action: 'index')
|
to(action: 'index')
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'accepts a symbol controller' do
|
it 'accepts a symbol controller' do
|
||||||
expect(controller_with_defined_routes).
|
expect(controller_with_defined_routes).
|
||||||
to route(:get, "/#{controller_name}").
|
to route(:get, "/#{controller_path}").
|
||||||
to(controller: controller_name.to_sym, action: 'index')
|
to(controller: controller_path.to_sym, action: 'index')
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'accepts a symbol action' do
|
it 'accepts a symbol action' do
|
||||||
expect(controller_with_defined_routes).
|
expect(controller_with_defined_routes).
|
||||||
to route(:get, "/#{controller_name}").
|
to route(:get, "/#{controller_path}").
|
||||||
to(action: :index)
|
to(action: :index)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -31,20 +31,20 @@ describe 'Shoulda::Matchers::ActionController::RouteMatcher', type: :controller
|
||||||
define_controller_with_defined_routes
|
define_controller_with_defined_routes
|
||||||
other_controller = define_controller('Other').new
|
other_controller = define_controller('Other').new
|
||||||
expect(other_controller).
|
expect(other_controller).
|
||||||
not_to route(:get, "/#{controller_name}").
|
not_to route(:get, "/#{controller_path}").
|
||||||
to(action: 'index')
|
to(action: 'index')
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'when route has parameters' do
|
context 'when route has parameters' do
|
||||||
it 'accepts a non-string parameter' do
|
it 'accepts a non-string parameter' do
|
||||||
expect(controller_with_defined_routes).
|
expect(controller_with_defined_routes).
|
||||||
to route(:get, "/#{controller_name}/1").
|
to route(:get, "/#{controller_path}/1").
|
||||||
to(action: 'show', id: 1)
|
to(action: 'show', id: 1)
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'rejects a route for different parameters' do
|
it 'rejects a route for different parameters' do
|
||||||
expect(controller_with_defined_routes).
|
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')
|
to(action: 'show', some: 'other', params: 'here')
|
||||||
end
|
end
|
||||||
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
|
context 'when controller and action are specified as a joined string' do
|
||||||
it 'accepts' do
|
it 'accepts' do
|
||||||
expect(controller_with_defined_routes).
|
expect(controller_with_defined_routes).
|
||||||
to route(:get, "/#{controller_name}").
|
to route(:get, "/#{controller_path}").
|
||||||
to("#{controller_name}#index")
|
to("#{controller_path}#index")
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'when route has parameters' do
|
context 'when route has parameters' do
|
||||||
it 'accepts a non-string parameter' do
|
it 'accepts a non-string parameter' do
|
||||||
expect(controller_with_defined_routes).
|
expect(controller_with_defined_routes).
|
||||||
to route(:get, "/#{controller_name}/1").
|
to route(:get, "/#{controller_path}/1").
|
||||||
to("#{controller_name}#show", id: 1)
|
to("#{controller_path}#show", id: 1)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def controller_with_defined_routes
|
def controller_with_defined_routes
|
||||||
@_controller_with_defined_routes ||= begin
|
@_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
|
define_routes do
|
||||||
get "/#{_controller_name}", to: "#{_controller_name}#index"
|
get "/#{_controller_path}", to: "#{_controller_path}#index"
|
||||||
get "/#{_controller_name}/:id", to: "#{_controller_name}#show"
|
get "/#{_controller_path}/:id", to: "#{_controller_path}#show"
|
||||||
end
|
end
|
||||||
|
|
||||||
controller
|
controller
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def controller_path
|
||||||
|
controller_name.sub(/Controller$/, '').underscore
|
||||||
|
end
|
||||||
|
|
||||||
alias_method :define_controller_with_defined_routes,
|
alias_method :define_controller_with_defined_routes,
|
||||||
:controller_with_defined_routes
|
:controller_with_defined_routes
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'given a controller with a defined glob url' do
|
context 'given a controller with a defined glob url' do
|
||||||
it 'accepts glob route' 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
|
define_routes do
|
||||||
get 'examples/*id', to: 'examples#example'
|
get '/examples/*id', to: 'examples#example'
|
||||||
end
|
end
|
||||||
|
|
||||||
expect(controller).to route(:get, '/examples/foo/bar').
|
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
|
context 'given a controller that is not namespaced' do
|
||||||
it_behaves_like 'a controller with a defined route' do
|
it_behaves_like 'a controller with a defined route' do
|
||||||
def controller
|
|
||||||
define_controller(controller_name).new
|
|
||||||
end
|
|
||||||
|
|
||||||
def controller_name
|
def controller_name
|
||||||
'examples'
|
'ExamplesController'
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'given a controller that is namespaced' do
|
context 'given a controller that is namespaced' do
|
||||||
it_behaves_like 'a controller with a defined route' do
|
it_behaves_like 'a controller with a defined route' do
|
||||||
def controller
|
before do
|
||||||
@_controller ||= begin
|
|
||||||
define_module('Admin')
|
define_module('Admin')
|
||||||
define_controller('Admin::Examples').new
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def controller_name
|
def controller_name
|
||||||
'admin/examples'
|
'Admin::ExamplesController'
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -37,7 +37,7 @@ describe Shoulda::Matchers::ActionController, type: :controller do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe Shoulda::Matchers::ActionController::StrongParametersMatcher do
|
describe Shoulda::Matchers::ActionController::StrongParametersMatcher, type: :controller do
|
||||||
describe '#description' do
|
describe '#description' do
|
||||||
it 'returns the correct string' do
|
it 'returns the correct string' do
|
||||||
options = { action: :create, method: :post }
|
options = { action: :create, method: :post }
|
||||||
|
|
|
@ -45,6 +45,10 @@ RSpec.configure do |config|
|
||||||
config.infer_spec_type_from_file_location!
|
config.infer_spec_type_from_file_location!
|
||||||
end
|
end
|
||||||
|
|
||||||
|
config.before(:all, type: :controller) do
|
||||||
|
self.class.controller(ApplicationController) { }
|
||||||
|
end
|
||||||
|
|
||||||
UnitTests::ActiveModelHelpers.configure_example_group(config)
|
UnitTests::ActiveModelHelpers.configure_example_group(config)
|
||||||
UnitTests::ActiveModelVersions.configure_example_group(config)
|
UnitTests::ActiveModelVersions.configure_example_group(config)
|
||||||
UnitTests::ActiveResourceBuilder.configure_example_group(config)
|
UnitTests::ActiveResourceBuilder.configure_example_group(config)
|
||||||
|
|
Loading…
Reference in New Issue