Skip to content

Tests are destructive to development database when run without explicit RAILS_ENV=test #688

@aaronlippold

Description

@aaronlippold

Problem Description

When running tests with bundle exec rspec, the tests appear to be destructive to the development database, requiring a database reset after each test run. This is an anti-pattern that significantly impacts developer productivity.

Expected Behavior

Tests should:

  1. Always use the test database (vulcan_vue_test) regardless of how they're invoked
  2. Never affect the development database (vulcan_vue_development)
  3. Not require a database reset after running tests

Current Behavior

  • Running tests seems to affect the development database
  • After running tests, rails db:reset is needed to restore the development database
  • Spring server and other processes hold database connections, making reset difficult

Investigation Needed

  1. Verify that ENV['RAILS_ENV'] is properly set to 'test' when running specs
  2. Check if DatabaseCleaner is somehow affecting the wrong database
  3. Investigate if there's a configuration issue causing tests to connect to development database
  4. Review if any specs are explicitly changing Rails.env or database connections

Configuration Details

  • Rails 8.0.2.1
  • Ruby 3.3.9
  • PostgreSQL
  • DatabaseCleaner configured with transaction/truncation strategies
  • Separate databases configured in database.yml:
    • Development: vulcan_vue_development
    • Test: vulcan_vue_test

Potential Causes

  1. Missing or incorrect ENV['RAILS_ENV'] = 'test' setting
  2. DatabaseCleaner misconfiguration
  3. Specs that manually connect to development database
  4. Spring or other preloaders interfering with environment detection
  5. Database URL environment variable overriding database.yml

Reproduction Steps

  1. Run bundle exec rspec in development
  2. Try to use the application
  3. Notice data corruption or missing data
  4. Need to run rails db:reset to fix

Workaround

Currently must explicitly set environment: RAILS_ENV=test bundle exec rspec

Priority

High - This significantly impacts developer experience and could lead to accidental data loss in development environments.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions