Skip to content

Allow load_fixture to switch fixture path for already-loaded models#79

Merged
byroot merged 1 commit intobyroot:masterfrom
danielahrnsbrak:fix-load-fixture-different-path
Mar 12, 2026
Merged

Allow load_fixture to switch fixture path for already-loaded models#79
byroot merged 1 commit intobyroot:masterfrom
danielahrnsbrak:fix-load-fixture-different-path

Conversation

@danielahrnsbrak
Copy link
Contributor

Summary

  • TestHelper.load_fixture now unloads and reloads when called for an already-cached model with a different alternate_base_path. Previously, the second call was silently ignored regardless of path, leaving stale data from the first load.
  • Calls with the same path remain a no-op (preserving the existing optimization).

Problem

When two different test setups call load_fixture for the same model class but with different fixture paths, the second call is silently skipped because the cache only checks @cache.key?(model_class) — it doesn't compare paths:

# Test A's setup:
FrozenRecord::TestHelper.load_fixture(ApiChange, "test/fixtures/webhooks")  # loads 27 records

# Test B's setup (different path, same model):
FrozenRecord::TestHelper.load_fixture(ApiChange, "test/fixtures/versioning")  # silently ignored — still 27 webhook records

This is a footgun in test suites where execution order is randomized and different test groups use different fixture directories for the same FrozenRecord model.

Fix

When load_fixture is called for a model already in the cache, compare the currently loaded base_path against the requested alternate_base_path. If they match, return early (no-op). If they differ, call unload_fixture first to restore the original base_path, then proceed with the new load. The original base_path (for unload_fixtures teardown) is preserved correctly because unload_fixture restores it before the re-load captures a new snapshot.

Tests

  • Updated the existing "ignores repeat calls" test to specifically assert same-path no-op behavior
  • Added test: switching to a different path loads the new fixture data
  • Added test: unload_fixtures still restores the original base_path after path switches

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).
@danielahrnsbrak danielahrnsbrak marked this pull request as ready for review March 12, 2026 19:08
@byroot byroot merged commit 932c432 into byroot:master Mar 12, 2026
8 of 12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants