From f655658bf37d4b4cf63429c7fdf93eef8b807eb7 Mon Sep 17 00:00:00 2001 From: Daniel Ahrnsbrak Date: Thu, 12 Mar 2026 15:04:26 -0400 Subject: [PATCH] Allow load_fixture to switch fixture path for already-loaded models When load_fixture is called for a model that is already cached but with a different alternate_base_path, it now unloads the previous fixture and loads from the new path. Previously, the second call was silently ignored regardless of path, leaving stale fixture data from the first call. Calls with the same path remain a no-op (preserving the existing optimization). --- lib/frozen_record/test_helper.rb | 5 ++++- spec/test_helper_spec.rb | 30 +++++++++++++++++++++++++++--- 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/lib/frozen_record/test_helper.rb b/lib/frozen_record/test_helper.rb index cb57bc1..b89b91b 100644 --- a/lib/frozen_record/test_helper.rb +++ b/lib/frozen_record/test_helper.rb @@ -10,7 +10,10 @@ def load_fixture(model_class, alternate_base_path) ensure_model_class_is_frozenrecord(model_class) - return if @cache.key?(model_class) + if @cache.key?(model_class) + return if model_class.base_path.to_s == alternate_base_path.to_s + unload_fixture(model_class) + end @cache[model_class] = base_path_if_file_present(model_class) diff --git a/spec/test_helper_spec.rb b/spec/test_helper_spec.rb index ff6520d..83becfa 100644 --- a/spec/test_helper_spec.rb +++ b/spec/test_helper_spec.rb @@ -24,18 +24,42 @@ }.to raise_error(ArgumentError) end - it 'uses test fixtures that were loaded first, and ignores repeat calls to load_fixture' do + it 'is a no-op when called again with the same path' do test_fixtures_base_path = File.join(File.dirname(__FILE__), 'fixtures', 'test_helper') FrozenRecord::TestHelper.load_fixture(Country, test_fixtures_base_path) expect(Country.count).to be == 1 - # Note: If we actually loaded this fixture, we'd expect 3 Countries to be loaded. Instead, we continue to get 3. - test_fixtures_base_path = File.join(File.dirname(__FILE__), 'fixtures') FrozenRecord::TestHelper.load_fixture(Country, test_fixtures_base_path) expect(Country.count).to be == 1 FrozenRecord::TestHelper.unload_fixtures end + + it 'switches to the new fixtures when called with a different path' do + test_fixtures_base_path = File.join(File.dirname(__FILE__), 'fixtures', 'test_helper') + FrozenRecord::TestHelper.load_fixture(Country, test_fixtures_base_path) + expect(Country.count).to be == 1 + + default_fixtures_base_path = File.join(File.dirname(__FILE__), 'fixtures') + FrozenRecord::TestHelper.load_fixture(Country, default_fixtures_base_path) + expect(Country.count).to be == 3 + + FrozenRecord::TestHelper.unload_fixtures + end + + it 'preserves the original base_path for unload after switching paths' do + original_base_path = Country.base_path + + test_fixtures_base_path = File.join(File.dirname(__FILE__), 'fixtures', 'test_helper') + FrozenRecord::TestHelper.load_fixture(Country, test_fixtures_base_path) + + default_fixtures_base_path = File.join(File.dirname(__FILE__), 'fixtures') + FrozenRecord::TestHelper.load_fixture(Country, default_fixtures_base_path) + + FrozenRecord::TestHelper.unload_fixtures + expect(Country.base_path).to eq(original_base_path) + expect(Country.count).to be == 3 + end end describe '.unload_fixture' do