2014-10-27 00:51:42 +00:00
|
|
|
require 'unit_spec_helper'
|
2010-12-13 22:28:59 +00:00
|
|
|
|
2014-06-27 22:39:39 +00:00
|
|
|
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).
|
2014-12-24 21:44:38 +00:00
|
|
|
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).
|
2014-12-24 21:44:38 +00:00
|
|
|
to route(:get, "/#{controller_path}").
|
|
|
|
to(controller: controller_path.to_sym, action: 'index')
|
2010-12-13 22:28:59 +00:00
|
|
|
end
|
|
|
|
|
2014-11-15 01:24:46 +00:00
|
|
|
it 'accepts a symbol action' do
|
|
|
|
expect(controller_with_defined_routes).
|
2014-12-24 21:44:38 +00:00
|
|
|
to route(:get, "/#{controller_path}").
|
2014-11-15 01:24:46 +00:00
|
|
|
to(action: :index)
|
|
|
|
end
|
2010-12-13 22:28:59 +00:00
|
|
|
|
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
|
2010-12-13 22:28:59 +00:00
|
|
|
|
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).
|
2014-12-24 21:44:38 +00:00
|
|
|
not_to route(:get, "/#{controller_path}").
|
2014-11-15 01:24:46 +00:00
|
|
|
to(action: 'index')
|
|
|
|
end
|
2010-12-13 22:28:59 +00:00
|
|
|
|
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).
|
2014-12-24 21:44:38 +00:00
|
|
|
to route(:get, "/#{controller_path}/1").
|
2014-11-15 01:24:46 +00:00
|
|
|
to(action: 'show', id: 1)
|
|
|
|
end
|
2010-12-13 22:28:59 +00:00
|
|
|
|
2014-11-15 01:24:46 +00:00
|
|
|
it 'rejects a route for different parameters' do
|
|
|
|
expect(controller_with_defined_routes).
|
2014-12-24 21:44:38 +00:00
|
|
|
not_to route(:get, "/#{controller_path}/1").
|
2014-11-15 01:24:46 +00:00
|
|
|
to(action: 'show', some: 'other', params: 'here')
|
|
|
|
end
|
|
|
|
end
|
2015-03-30 23:14:33 +00:00
|
|
|
|
|
|
|
context 'when route has a default format' do
|
|
|
|
it 'accepts' do
|
|
|
|
expect(controller_with_defined_routes).
|
|
|
|
to route(:post, "/#{controller_path}").
|
|
|
|
to(action: 'create', format: 'json')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'accepts when format is specified as a symbol' do
|
|
|
|
expect(controller_with_defined_routes).
|
|
|
|
to route(:post, "/#{controller_path}").
|
|
|
|
to(action: 'create', format: :json)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'rejects when format is unspecified' do
|
|
|
|
expect(controller_with_defined_routes).
|
|
|
|
not_to route(:post, "/#{controller_path}").
|
|
|
|
to(action: 'create')
|
|
|
|
end
|
|
|
|
end
|
2010-12-13 22:28:59 +00:00
|
|
|
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).
|
2014-12-24 21:44:38 +00:00
|
|
|
to route(:get, "/#{controller_path}").
|
|
|
|
to("#{controller_path}#index")
|
2014-11-15 01:24:46 +00:00
|
|
|
end
|
2014-11-18 09:22:39 +00:00
|
|
|
|
|
|
|
context 'when route has parameters' do
|
|
|
|
it 'accepts a non-string parameter' do
|
|
|
|
expect(controller_with_defined_routes).
|
2014-12-24 21:44:38 +00:00
|
|
|
to route(:get, "/#{controller_path}/1").
|
|
|
|
to("#{controller_path}#show", id: 1)
|
2014-11-18 09:22:39 +00:00
|
|
|
end
|
|
|
|
end
|
2015-03-30 23:14:33 +00:00
|
|
|
|
|
|
|
context 'when route has the format' do
|
|
|
|
it 'accepts' do
|
|
|
|
expect(controller_with_defined_routes).
|
|
|
|
to route(:post, "/#{controller_path}").
|
|
|
|
to("#{controller_path}#create", format: 'json')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'rejects when format is unspecified' do
|
|
|
|
expect(controller_with_defined_routes).
|
|
|
|
not_to route(:post, "/#{controller_path}").
|
|
|
|
to(action: 'create')
|
|
|
|
end
|
|
|
|
end
|
2010-12-13 22:28:59 +00:00
|
|
|
end
|
|
|
|
|
2014-11-15 01:24:46 +00:00
|
|
|
def controller_with_defined_routes
|
|
|
|
@_controller_with_defined_routes ||= begin
|
2014-12-24 21:44:38 +00:00
|
|
|
controller_class = define_controller(controller_name)
|
|
|
|
_controller_path = controller_path
|
|
|
|
|
|
|
|
setup_rails_controller_test(controller_class)
|
2010-12-13 22:28:59 +00:00
|
|
|
|
2014-11-15 01:24:46 +00:00
|
|
|
define_routes do
|
2014-12-24 21:44:38 +00:00
|
|
|
get "/#{_controller_path}", to: "#{_controller_path}#index"
|
|
|
|
get "/#{_controller_path}/:id", to: "#{_controller_path}#show"
|
2015-03-30 23:14:33 +00:00
|
|
|
post "/#{_controller_path}",
|
|
|
|
to: "#{_controller_path}#create",
|
|
|
|
defaults: { format: :json }
|
2014-11-15 01:24:46 +00:00
|
|
|
end
|
2010-12-13 22:28:59 +00:00
|
|
|
|
2014-11-15 01:24:46 +00:00
|
|
|
controller
|
|
|
|
end
|
2010-12-13 22:28:59 +00:00
|
|
|
end
|
2012-12-20 05:04:27 +00:00
|
|
|
|
2014-12-24 21:44:38 +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
|
2014-12-24 21:44:38 +00:00
|
|
|
controller_class = define_controller('Examples')
|
|
|
|
setup_rails_controller_test(controller_class)
|
2013-10-15 22:12:17 +00:00
|
|
|
|
2012-12-20 05:04:27 +00:00
|
|
|
define_routes do
|
2014-12-24 21:44:38 +00:00
|
|
|
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
|
2014-12-24 21:44:38 +00:00
|
|
|
'ExamplesController'
|
2014-11-15 01:24:46 +00:00
|
|
|
end
|
2012-12-20 05:04:27 +00:00
|
|
|
end
|
2010-12-13 22:28:59 +00:00
|
|
|
end
|
2014-11-18 09:23:04 +00:00
|
|
|
|
|
|
|
context 'given a controller that is namespaced' do
|
|
|
|
it_behaves_like 'a controller with a defined route' do
|
2014-12-24 21:44:38 +00:00
|
|
|
before do
|
|
|
|
define_module('Admin')
|
2014-11-18 09:23:04 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def controller_name
|
2014-12-24 21:44:38 +00:00
|
|
|
'Admin::ExamplesController'
|
2014-11-18 09:23:04 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2010-12-13 22:28:59 +00:00
|
|
|
end
|