1
0
Fork 0
mirror of https://github.com/thoughtbot/shoulda-matchers.git synced 2022-11-09 12:01:38 -05:00

The route matcher accepts a format as a symbol

This commit is contained in:
Mauro George 2015-03-30 20:14:33 -03:00 committed by Elliot Winkler
parent a5bc5806ff
commit 15359eb6ec
4 changed files with 60 additions and 6 deletions

View file

@ -58,6 +58,9 @@
* Fix `validate_uniqueness_of` + `scoped_to` so that it does not raise an error
if a record exists where the scoped attribute is nil. ([#677])
* Fix `route` matcher so if your route includes a default `format`, you can
specify this as a symbol or string. ([#693])
### Features
* Add `on` qualifier to `permit`. This allows you to make an assertion that
@ -78,6 +81,7 @@
[#675]: https://github.com/thoughtbot/shoulda-matchers/pull/675
[#677]: https://github.com/thoughtbot/shoulda-matchers/pull/677
[#620]: https://github.com/thoughtbot/shoulda-matchers/pull/620
[#693]: https://github.com/thoughtbot/shoulda-matchers/pull/693
# 2.8.0

View file

@ -79,6 +79,10 @@ module Shoulda
#
# route(:get, '/posts/1').to('posts#show', id: 1)
#
# You may also specify special parameters such as `:format`:
#
# route(:get, '/posts').to('posts#index', format: :json)
#
# @return [RouteMatcher]
#
def route(method, path)

View file

@ -3,6 +3,8 @@ module Shoulda
module ActionController
# @private
class RouteParams
PARAMS_TO_SYMBOLIZE = %i{ format }
def initialize(args)
@args = args
end
@ -26,17 +28,24 @@ module Shoulda
def extract_params_from_string
controller, action = args[0].split('#')
params = (args[1] || {}).merge(controller: controller, action: action)
stringify_values(params)
normalize_values(params)
end
def stringify_params
stringify_values(args[0])
normalize_values(args[0])
end
def stringify_values(hash)
hash.inject({}) do |hash_copy, (key, value)|
hash_copy[key] = stringify(value)
hash_copy
def normalize_values(hash)
hash.each_with_object({}) do |(key, value), hash_copy|
hash_copy[key] = symbolize_or_stringify(key, value)
end
end
def symbolize_or_stringify(key, value)
if key.in?(PARAMS_TO_SYMBOLIZE)
value.to_sym
else
stringify(value)
end
end

View file

@ -48,6 +48,26 @@ describe 'Shoulda::Matchers::ActionController::RouteMatcher', type: :controller
to(action: 'show', some: 'other', params: 'here')
end
end
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
end
context 'when controller and action are specified as a joined string' do
@ -64,6 +84,20 @@ describe 'Shoulda::Matchers::ActionController::RouteMatcher', type: :controller
to("#{controller_path}#show", id: 1)
end
end
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
end
def controller_with_defined_routes
@ -76,6 +110,9 @@ describe 'Shoulda::Matchers::ActionController::RouteMatcher', type: :controller
define_routes do
get "/#{_controller_path}", to: "#{_controller_path}#index"
get "/#{_controller_path}/:id", to: "#{_controller_path}#show"
post "/#{_controller_path}",
to: "#{_controller_path}#create",
defaults: { format: :json }
end
controller