Skip to content

Commit

Permalink
Reset AR model cache between tests
Browse files Browse the repository at this point in the history
When models are created fresh for each test the configuration may be cached by
ActiveSupport. This results in tests failing if they are executed after others
that use the same model names with different associations. The method for
caching this information was changed for Rails 7 meaning that is is no longer
necessary to explicitly clear the cache. The change was made here:

  rails/rails@14d4edd
  • Loading branch information
jrmhaig committed Jul 29, 2021
1 parent f558595 commit 464c3db
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 0 deletions.
2 changes: 2 additions & 0 deletions spec/lib/amoeba/config/has_many_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
t.boolean :extra
end

reset_model_cache
stub_const 'Parent', Class.new(ActiveRecord::Base)
stub_const 'Child', Class.new(ActiveRecord::Base)
Parent.class_eval <<~CONFIG, __FILE__, __LINE__ + 1
Expand Down Expand Up @@ -127,6 +128,7 @@
t.references :child
end

reset_model_cache
stub_const 'Parent', Class.new(ActiveRecord::Base)
stub_const 'Child', Class.new(ActiveRecord::Base)
stub_const 'ParentChild', Class.new(ActiveRecord::Base)
Expand Down
1 change: 1 addition & 0 deletions spec/lib/amoeba/config/has_one_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
t.string :name
end

reset_model_cache
stub_const 'Parent', Class.new(ActiveRecord::Base)
stub_const 'Child', Class.new(ActiveRecord::Base)

Expand Down
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

require 'active_record'
require 'amoeba'
require 'support/helpers'

adapter = if defined?(JRuby)
require 'activerecord-jdbcsqlite3-adapter'
Expand Down
5 changes: 5 additions & 0 deletions spec/support/helpers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# frozen_string_literal: true

def reset_model_cache
ActiveSupport::Dependencies::Reference.clear! if ActiveSupport::VERSION::MAJOR < 7
end

0 comments on commit 464c3db

Please sign in to comment.