Fix flaky VolunteerPolicy spec by making org assignments explicit#6771
Merged
compwron merged 1 commit intorubyforgood:mainfrom Mar 13, 2026
Merged
Conversation
The volunteer policy spec relied on implicit casa_org assignment from the factory default (CasaOrg.first || create(:casa_org)), which hits the database even inside build_stubbed. When FactoryBot's auto- incrementing stub IDs collide with real DB record IDs, ActiveRecord's equality check (class + ID) causes same_org? to return incorrect results non-deterministically. Introduce explicit let(:casa_org) and let(:other_org) shared across all stubbed users and records, bypassing the factory's DB-hitting default entirely and guaranteeing deterministic org identity. Resolves #6770
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes the flaky test
VolunteerPolicy edit? different org when user is a supervisor does not allowby making allcasa_orgassignments explicit in the spec.Root cause: The user factory default
casa_org { CasaOrg.first || create(:casa_org) }hits the database even insidebuild_stubbed. When FactoryBot's auto-incrementing stub IDs collide with real DB record IDs, ActiveRecord's==(which compares class + ID) causessame_org?to return incorrect results non-deterministically.Fix: Introduce shared
let(:casa_org)andlet(:other_org)blocks, passed explicitly to all stubbed users and records. This bypasses the factory's DB-hitting default and guarantees deterministic org identity.Resolves #6770
Test Plan
bundle exec rspec spec/policies/volunteer_policy_spec.rb— 18 examples, 0 failures