From 4df806f95f8c64c8a1406aebf5f82807ff76c611 Mon Sep 17 00:00:00 2001 From: eileencodes Date: Wed, 18 Mar 2015 08:04:21 -0400 Subject: Improve test for leaky scope chain This is a better test for 51660f0. It is testing that the SQL is the same before and after the previously leaky scope is called. Before if `hotel.drink_designers` was called first then `hotel.recipes` would incorrectly get the scope applied. We want to be sure that the polymorphic hm:t association is not leaking into or affecting the SQL for the hm:t association on `Hotel`. The reason I couldn't do this before was because there was an issue with the SQL getting cached and wanted to resolve that later and then fix the test to be better. Because of the caching, this test requires that `Hotel.reflect_on_association(:recipes).clear_association_scope_cache` be called after the first call to `hotel.recipes` to clear the assocation scope chain and not interfere with the rest of the test. --- activerecord/test/cases/reflection_test.rb | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'activerecord') diff --git a/activerecord/test/cases/reflection_test.rb b/activerecord/test/cases/reflection_test.rb index 06bc70c172..7b47c80331 100644 --- a/activerecord/test/cases/reflection_test.rb +++ b/activerecord/test/cases/reflection_test.rb @@ -284,8 +284,14 @@ class ReflectionTest < ActiveRecord::TestCase drink = department.chefs.create!(employable: DrinkDesigner.create!) Recipe.create!(chef_id: drink.id, hotel_id: hotel.id) + expected_sql = capture_sql { hotel.recipes.to_a } + + Hotel.reflect_on_association(:recipes).clear_association_scope_cache + hotel.reload hotel.drink_designers.to_a - assert_sql(/^(?!.*employable_type).*$/) { hotel.recipes.to_a } + loaded_sql = capture_sql { hotel.recipes.to_a } + + assert_equal expected_sql, loaded_sql end def test_nested? -- cgit v1.2.3