From 1388a905e0fbf4a5219205239c8c776742f1718b Mon Sep 17 00:00:00 2001 From: Benjamin Fleischer Date: Tue, 10 Nov 2015 01:13:46 -0600 Subject: [PATCH] Test keyword args in requests for Rails 5; fallback for earlier versions kwargs: params,session,flash,method,body Fix Rails 5 DEPRECATION WARNING --- .../json_api/pagination_test.rb | 14 ++++----- test/support/rails5_shims.rb | 29 +++++++++++++++++++ test/support/test_case.rb | 6 ++++ test/test_helper.rb | 2 ++ 4 files changed, 44 insertions(+), 7 deletions(-) create mode 100644 test/support/rails5_shims.rb diff --git a/test/action_controller/json_api/pagination_test.rb b/test/action_controller/json_api/pagination_test.rb index 4286ed886..7c8ebec26 100644 --- a/test/action_controller/json_api/pagination_test.rb +++ b/test/action_controller/json_api/pagination_test.rb @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/test/support/rails5_shims.rb b/test/support/rails5_shims.rb new file mode 100644 index 000000000..ebe0ceaff --- /dev/null +++ b/test/support/rails5_shims.rb @@ -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 diff --git a/test/support/test_case.rb b/test/support/test_case.rb index c07b5bc00..8f1afe79a 100644 --- a/test/support/test_case.rb +++ b/test/support/test_case.rb @@ -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 diff --git a/test/test_helper.rb b/test/test_helper.rb index f494eba12..9d0698207 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -64,6 +64,8 @@ def Minitest.after_run(&block) require 'support/serialization_testing' +require 'support/rails5_shims' + require 'fixtures/active_record' require 'fixtures/poro'