Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion lib/frozen_record/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
30 changes: 27 additions & 3 deletions spec/test_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading