From df6c0ef56e52b17655d62290e4618b9c2ba46a2c Mon Sep 17 00:00:00 2001 From: Elliot Winkler Date: Thu, 6 Nov 2014 10:28:13 -0700 Subject: [PATCH] Clear schema cache after each test When running unit tests in a random order, some tests may fail as the columns hash within a model may not be in sync with the database. This is because the columns hash is cached outside of the model, so removing the model from the object space and then recreating that model won't clear the columns hash. --- spec/support/unit/helpers/class_builder.rb | 8 -------- spec/support/unit/helpers/model_builder.rb | 13 +++++++++++++ 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/spec/support/unit/helpers/class_builder.rb b/spec/support/unit/helpers/class_builder.rb index fff1290c..bcea6d1e 100644 --- a/spec/support/unit/helpers/class_builder.rb +++ b/spec/support/unit/helpers/class_builder.rb @@ -60,10 +60,6 @@ module UnitTests if block_given? constant.class_eval(&block) end - - if constant.respond_to?(:reset_column_information) - constant.reset_column_information - end end end @@ -72,9 +68,5 @@ module UnitTests def teardown_defined_constants ActiveSupport::Dependencies.clear end - - def teardown_defined_constants - ActiveSupport::Dependencies.clear - end end end diff --git a/spec/support/unit/helpers/model_builder.rb b/spec/support/unit/helpers/model_builder.rb index fb2d2a5d..643259e1 100644 --- a/spec/support/unit/helpers/model_builder.rb +++ b/spec/support/unit/helpers/model_builder.rb @@ -8,6 +8,7 @@ module UnitTests example_group.include(self) example_group.after do + clear_column_caches drop_created_tables end end @@ -76,6 +77,18 @@ module UnitTests private + def clear_column_caches + # Rails 3.1 - 4.0 + if ActiveRecord::Base.connection_pool.respond_to?(:clear_cache!) + ActiveRecord::Base.connection_pool.clear_cache! + end + + # Rails 4.x + if ActiveRecord::Base.connection.respond_to?(:schema_cache) + ActiveRecord::Base.connection.schema_cache.clear! + end + end + def drop_created_tables connection = ActiveRecord::Base.connection