1+ # Ruby 3.1+ moved Logger to a separate gem and concurrent-ruby no longer requires
2+ # it, so it must be loaded before ActiveSupport or ActiveSupport::LoggerThreadSafeLevel
3+ # raises `uninitialized constant Logger` on Rails < 7.1.
4+ require 'logger'
15require 'simplecov'
26require 'database_cleaner'
37
2024end
2125
2226require 'active_record/railtie'
23- require 'rails/test_help'
27+ require 'action_controller/railtie'
28+ # NOTE: `rails/test_help` is required after `TestApp.initialize!` (below). Since
29+ # Rails 8.1 it reads `Rails.application.config` at load time, so it cannot be
30+ # required before an application exists.
2431require 'minitest/mock'
2532require 'jsonapi-resources'
2633require 'pry'
@@ -52,14 +59,18 @@ class TestApp < Rails::Application
5259 config . action_controller . action_on_unpermitted_parameters = :raise
5360
5461 ActiveRecord ::Schema . verbose = false
55- config . active_record . schema_format = :none
62+ # The test schema is defined in test/fixtures/active_record.rb and loaded by
63+ # hand, so Rails must not try to maintain or verify it. This used to be spelled
64+ # `schema_format = :none`, but Rails 8.1 rejects anything but :ruby / :sql.
65+ config . active_record . maintain_test_schema = false
5666 config . active_support . test_order = :random
5767
5868 if Rails ::VERSION ::MAJOR >= 5
5969 config . active_support . halt_callback_chains_on_return_false = false
6070 config . active_record . time_zone_aware_types = [ :time , :datetime ]
6171 config . active_record . belongs_to_required_by_default = false
62- if Rails ::VERSION ::MINOR >= 2
72+ # Removed in Rails 6.1; only ever needed on 5.2.
73+ if Rails ::VERSION ::MAJOR == 5 && Rails ::VERSION ::MINOR >= 2
6374 config . active_record . sqlite3 . represent_boolean_as_integer = true
6475 end
6576 end
@@ -80,7 +91,10 @@ class Engine < ::Rails::Engine
8091# Monkeypatch ActionController::TestCase to delete the RAW_POST_DATA on subsequent calls in the same test.
8192if Rails ::VERSION ::MAJOR >= 5
8293 module ClearRawPostHeader
83- def process ( action , *args )
94+ # `process` takes keyword arguments (method:, params:, ...), so they must be
95+ # captured and forwarded as keywords rather than collapsed into a positional
96+ # hash by `*args`.
97+ def process ( action , *args , **kwargs )
8498 @request . delete_header 'RAW_POST_DATA'
8599 super
86100 end
@@ -210,6 +224,8 @@ def show_queries
210224
211225TestApp . initialize!
212226
227+ require 'rails/test_help'
228+
213229require File . expand_path ( '../fixtures/active_record' , __FILE__ )
214230
215231module Pets
@@ -443,6 +459,17 @@ class CatResource < JSONAPI::Resource
443459# Ensure backward compatibility with Minitest 4
444460Minitest ::Test = MiniTest ::Unit ::TestCase unless defined? ( Minitest ::Test )
445461
462+ # `fixture_path` (singular) was deprecated in Rails 7.1 and removed in Rails 8.0
463+ # in favour of `fixture_paths` (plural, an array).
464+ def set_fixture_path ( klass )
465+ path = "#{ Rails . root } /fixtures"
466+ if klass . respond_to? ( :fixture_paths= )
467+ klass . fixture_paths = [ path ]
468+ else
469+ klass . fixture_path = path
470+ end
471+ end
472+
446473class Minitest ::Test
447474 include Helpers ::Assertions
448475 include Helpers ::ValueMatchers
@@ -454,20 +481,20 @@ def run_in_transaction?
454481 true
455482 end
456483
457- self . fixture_path = " #{ Rails . root } /fixtures"
484+ set_fixture_path ( self )
458485 fixtures :all
459486end
460487
461488class ActiveSupport ::TestCase
462- self . fixture_path = " #{ Rails . root } /fixtures"
489+ set_fixture_path ( self )
463490 fixtures :all
464491 setup do
465492 @routes = TestApp . routes
466493 end
467494end
468495
469496class ActionDispatch ::IntegrationTest
470- self . fixture_path = " #{ Rails . root } /fixtures"
497+ set_fixture_path ( self )
471498 fixtures :all
472499
473500 def assert_jsonapi_response ( expected_status , msg = nil )
@@ -519,13 +546,16 @@ def assert_cacheable_jsonapi_get(url, cached_classes = :all)
519546end
520547
521548class ActionController ::TestCase
522- def assert_cacheable_get ( action , *args )
549+ # `get` takes keyword arguments (params:, headers:, ...), so they must be
550+ # captured and forwarded as keywords rather than collapsed into a positional
551+ # hash by `*args`.
552+ def assert_cacheable_get ( action , *args , **kwargs )
523553 assert_nil JSONAPI . configuration . resource_cache
524554
525555 normal_queries = [ ]
526556 normal_query_callback = lambda { |_ , _ , _ , _ , payload | normal_queries . push payload [ :sql ] }
527557 ActiveSupport ::Notifications . subscribed ( normal_query_callback , 'sql.active_record' ) do
528- get action , *args
558+ get action , *args , ** kwargs
529559 end
530560 non_caching_response = json_response_sans_backtraces
531561 non_caching_status = response . status
@@ -559,7 +589,7 @@ def assert_cacheable_get(action, *args)
559589 @controller = nil
560590 setup_controller_request_and_response
561591 @request . headers . merge! ( orig_request_headers . dup )
562- get action , *args
592+ get action , *args , ** kwargs
563593 end
564594 end
565595 rescue Exception
0 commit comments