thoughtbot--shoulda-matchers/spec/unit/shoulda/matchers/action_controller/route_matcher_spec.rb

127 lines
3.7 KiB
Ruby
Raw Normal View History

require 'unit_spec_helper'
describe 'Shoulda::Matchers::ActionController::RouteMatcher', type: :controller do
2014-11-15 01:24:46 +00:00
shared_examples_for 'a controller with a defined route' do
context 'when controller and action are specified as explicit options' do
it 'accepts' do
expect(controller_with_defined_routes).
to route(:get, "/#{controller_path}").
2014-11-15 01:24:46 +00:00
to(action: 'index')
end
2012-03-16 16:15:23 +00:00
2014-11-15 01:24:46 +00:00
it 'accepts a symbol controller' do
expect(controller_with_defined_routes).
to route(:get, "/#{controller_path}").
to(controller: controller_path.to_sym, action: 'index')
end
2014-11-15 01:24:46 +00:00
it 'accepts a symbol action' do
expect(controller_with_defined_routes).
to route(:get, "/#{controller_path}").
2014-11-15 01:24:46 +00:00
to(action: :index)
end
2014-11-15 01:24:46 +00:00
it 'rejects an undefined route' do
expect(controller_with_defined_routes).
not_to route(:get, '/non_existent_route').
to(action: 'non_existent')
end
2014-11-15 01:24:46 +00:00
it 'rejects a route for another controller' do
define_controller_with_defined_routes
other_controller = define_controller('Other').new
expect(other_controller).
not_to route(:get, "/#{controller_path}").
2014-11-15 01:24:46 +00:00
to(action: 'index')
end
2014-11-15 01:24:46 +00:00
context 'when route has parameters' do
it 'accepts a non-string parameter' do
expect(controller_with_defined_routes).
to route(:get, "/#{controller_path}/1").
2014-11-15 01:24:46 +00:00
to(action: 'show', id: 1)
end
2014-11-15 01:24:46 +00:00
it 'rejects a route for different parameters' do
expect(controller_with_defined_routes).
not_to route(:get, "/#{controller_path}/1").
2014-11-15 01:24:46 +00:00
to(action: 'show', some: 'other', params: 'here')
end
end
end
2014-11-15 01:24:46 +00:00
context 'when controller and action are specified as a joined string' do
it 'accepts' do
expect(controller_with_defined_routes).
to route(:get, "/#{controller_path}").
to("#{controller_path}#index")
2014-11-15 01:24:46 +00:00
end
context 'when route has parameters' do
it 'accepts a non-string parameter' do
expect(controller_with_defined_routes).
to route(:get, "/#{controller_path}/1").
to("#{controller_path}#show", id: 1)
end
end
end
2014-11-15 01:24:46 +00:00
def controller_with_defined_routes
@_controller_with_defined_routes ||= begin
controller_class = define_controller(controller_name)
_controller_path = controller_path
setup_rails_controller_test(controller_class)
2014-11-15 01:24:46 +00:00
define_routes do
get "/#{_controller_path}", to: "#{_controller_path}#index"
get "/#{_controller_path}/:id", to: "#{_controller_path}#show"
2014-11-15 01:24:46 +00:00
end
2014-11-15 01:24:46 +00:00
controller
end
end
2012-12-20 05:04:27 +00:00
def controller_path
controller_name.sub(/Controller$/, '').underscore
end
2014-11-15 01:24:46 +00:00
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_class = define_controller('Examples')
setup_rails_controller_test(controller_class)
2012-12-20 05:04:27 +00:00
define_routes do
get '/examples/*id', to: 'examples#example'
2012-12-20 05:04:27 +00:00
end
2014-11-15 01:24:46 +00:00
expect(controller).to route(:get, '/examples/foo/bar').
to(action: 'example', id: 'foo/bar')
end
end
context 'given a controller that is not namespaced' do
it_behaves_like 'a controller with a defined route' do
def controller_name
'ExamplesController'
2014-11-15 01:24:46 +00:00
end
2012-12-20 05:04:27 +00:00
end
end
context 'given a controller that is namespaced' do
it_behaves_like 'a controller with a defined route' do
before do
define_module('Admin')
end
def controller_name
'Admin::ExamplesController'
end
end
end
end