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