Skip to content

Fix flaky VolunteerPolicy spec by making org assignments explicit#6771

Merged
compwron merged 1 commit intorubyforgood:mainfrom
costajohnt:fix/flaky-volunteer-policy-spec
Mar 13, 2026
Merged

Fix flaky VolunteerPolicy spec by making org assignments explicit#6771
compwron merged 1 commit intorubyforgood:mainfrom
costajohnt:fix/flaky-volunteer-policy-spec

Conversation

@costajohnt
Copy link
Contributor

Summary

Fixes the flaky test VolunteerPolicy edit? different org when user is a supervisor does not allow by making all casa_org assignments explicit in the spec.

Root cause: The user factory default casa_org { CasaOrg.first || create(:casa_org) } hits the database even inside build_stubbed. When FactoryBot's auto-incrementing stub IDs collide with real DB record IDs, ActiveRecord's == (which compares class + ID) causes same_org? to return incorrect results non-deterministically.

Fix: Introduce shared let(:casa_org) and let(: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
  • No new rubocop offenses introduced

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
@github-actions github-actions bot added ruby Pull requests that update Ruby code Tests! 🎉💖👏 labels Mar 12, 2026
@costajohnt costajohnt marked this pull request as ready for review March 12, 2026 15:16
Copy link
Collaborator

@compwron compwron left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice :)

@compwron compwron merged commit ac44c7c into rubyforgood:main Mar 13, 2026
11 checks passed
@costajohnt costajohnt deleted the fix/flaky-volunteer-policy-spec branch March 13, 2026 15:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ruby Pull requests that update Ruby code Tests! 🎉💖👏

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Flaky test: VolunteerPolicy edit? different org when user is a supervisor does not allow

2 participants