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.
This commit is contained in:
Elliot Winkler 2014-11-06 10:28:13 -07:00
parent b4bf814b3b
commit df6c0ef56e
2 changed files with 13 additions and 8 deletions

View File

@ -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

View File

@ -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