Skip to content

Commit

Permalink
Test keyword args in requests for Rails 5; fallback for earlier versions
Browse files Browse the repository at this point in the history
kwargs: params,session,flash,method,body

Fix Rails 5 DEPRECATION WARNING
  • Loading branch information
bf4 committed Nov 24, 2015
1 parent e568c8f commit 1388a90
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 7 deletions.
14 changes: 7 additions & 7 deletions test/action_controller/json_api/pagination_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def test_render_pagination_links_with_will_paginate
'next' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1",
'last' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1" }

get :render_pagination_using_will_paginate, page: { number: 2, size: 1 }
get :render_pagination_using_will_paginate, params: { page: { number: 2, size: 1 } }
response = JSON.parse(@response.body)
assert_equal expected_links, response['links']
end
Expand All @@ -62,7 +62,7 @@ def test_render_only_last_and_next_pagination_links
expected_links = { 'self' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=2",
'next' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=2",
'last' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=2" }
get :render_pagination_using_will_paginate, page: { number: 1, size: 2 }
get :render_pagination_using_will_paginate, params: { page: { number: 1, size: 2 } }
response = JSON.parse(@response.body)
assert_equal expected_links, response['links']
end
Expand All @@ -73,7 +73,7 @@ def test_render_pagination_links_with_kaminari
'prev' => "#{KAMINARI_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1",
'next' => "#{KAMINARI_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1",
'last' => "#{KAMINARI_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1" }
get :render_pagination_using_kaminari, page: { number: 2, size: 1 }
get :render_pagination_using_kaminari, params: { page: { number: 2, size: 1 } }
response = JSON.parse(@response.body)
assert_equal expected_links, response['links']
end
Expand All @@ -82,7 +82,7 @@ def test_render_only_prev_and_first_pagination_links
expected_links = { 'self' => "#{KAMINARI_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1",
'first' => "#{KAMINARI_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1",
'prev' => "#{KAMINARI_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=1" }
get :render_pagination_using_kaminari, page: { number: 3, size: 1 }
get :render_pagination_using_kaminari, params: { page: { number: 3, size: 1 } }
response = JSON.parse(@response.body)
assert_equal expected_links, response['links']
end
Expand All @@ -91,7 +91,7 @@ def test_render_only_last_and_next_pagination_links_with_additional_params
expected_links = { 'self' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=2&teste=additional",
'next' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=2&teste=additional",
'last' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=2&teste=additional" }
get :render_pagination_using_will_paginate, page: { number: 1, size: 2 }, teste: 'additional'
get :render_pagination_using_will_paginate, params: { page: { number: 1, size: 2 }, teste: 'additional' }
response = JSON.parse(@response.body)
assert_equal expected_links, response['links']
end
Expand All @@ -100,13 +100,13 @@ def test_render_only_prev_and_first_pagination_links_with_additional_params
expected_links = { 'self' => "#{KAMINARI_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1&teste=additional",
'first' => "#{KAMINARI_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1&teste=additional",
'prev' => "#{KAMINARI_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=1&teste=additional" }
get :render_pagination_using_kaminari, page: { number: 3, size: 1 }, teste: 'additional'
get :render_pagination_using_kaminari, params: { page: { number: 3, size: 1 }, teste: 'additional' }
response = JSON.parse(@response.body)
assert_equal expected_links, response['links']
end

def test_array_without_pagination_links
get :render_array_without_pagination_links, page: { number: 2, size: 1 }
get :render_array_without_pagination_links, params: { page: { number: 2, size: 1 } }
response = JSON.parse(@response.body)
refute response.key? 'links'
end
Expand Down
29 changes: 29 additions & 0 deletions test/support/rails5_shims.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
module Rails5Shims
module ControllerTests
# https://github.com/rails/rails/blob/b217354/actionpack/lib/action_controller/test_case.rb
REQUEST_KWARGS = %i(params session flash method body xhr)

# Fold kwargs from test request into args
# Band-aid for DEPRECATION WARNING
def get(path, *args)
hash = args && args[0]
if hash.respond_to?(:key)
Rails5Shims::ControllerTests::REQUEST_KWARGS.each do |kwarg|
next unless hash.key?(kwarg)
hash.merge! hash.delete(kwarg)
end
end
super
end

# Uncomment for debugging where the kwargs warnings come from
# def non_kwarg_request_warning
# super.tap do
# STDOUT.puts caller[2..3]
# end
# end
end
end
if Rails::VERSION::MAJOR < 5
ActionController::TestCase.send :include, Rails5Shims::ControllerTests
end
6 changes: 6 additions & 0 deletions test/support/test_case.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,10 @@ def assigns(key = nil)
@controller.view_assigns.each { |k, v| assigns.regular_writer(k, v) }
key.nil? ? assigns : assigns[key]
end

# Rails5: Uncomment for debugging where the warnings come from
# def non_kwarg_request_warning
# super
# STDOUT.puts caller[2..3]
# end
end
2 changes: 2 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ def Minitest.after_run(&block)

require 'support/serialization_testing'

require 'support/rails5_shims'

require 'fixtures/active_record'

require 'fixtures/poro'
Expand Down

0 comments on commit 1388a90

Please sign in to comment.