1

I have been running Rspec recently but realised it was completely deleting my development database. I believe the cause of this is the failure of setting 'RAILS_ENV' at the beginning of 'rails_helper.rb' and therefore database cleaner is clearing the development db as well as the test db. Unsure if this is actually what is happening.

In order to combat the issue of

[1] RAILS_ENV
=> nil
[2] ENV['RAILS_ENV']
=> nil

I decided to set ENV['RAILS_ENV'] at the beginning of the file but this appears to break everything

require 'spec_helper'
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../config/environment', __dir__)

after some digging, it's a call to 'Bundle.require(*Rails.groups)' that actually breaks and the response is

[1] Bundle.require(*Rails.groups)
=> NameError: uninitialized constant FactoryBotFactory::BaseFactory

I am calling this prior to loading the application due to an issue of setting constants throughout the application rather than using "ENV['SOMETHING']". This works fine locally and in production and even works for rspec... but not if I set 'ENV['RAILS_ENV']'

application.rb

Bundler.require(*Rails.groups)
Dotenv::Railtie.load unless Rails.env.production?
require_relative 'constants'
module MyApp
  class Application < Rails::Application
bubbaspaarx
  • 616
  • 4
  • 15
  • looks like `factory_bot_factory` is broken. i'm guessing it's in your Gemfile's `group :test` that was never loading until you set rails_env to test. by the looks of it you're not even using that gem. – Alex Jul 08 '22 at 07:15
  • It definitely does work without the setting of ENV['RAILS_ENV'] ||= 'test'. It just means that DatabaseCleaner clears the development db as well. – bubbaspaarx Jul 08 '22 at 09:00

1 Answers1

0

For anyone else that may come across this problem.

We found that there is an additional configuration available for DatabaseCleaner when using active record.

When you use the standard setup, database cleaner is set to

 DatabaseCleaner[:active_record].db = :default

which i believe is then setting the db as development. We added this line directly after the require statement for the gem and it appears to be working find.

  require 'database_cleaner-active_record'

  DatabaseCleaner[:active_record].db = :test
  
  config.before(:suite) do
    DatabaseCleaner.strategy = :transaction
    DatabaseCleaner.clean_with(:truncation)
  end

We also removed the line

ENV['RAILS_ENV'] = 'test'
bubbaspaarx
  • 616
  • 4
  • 15