Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DatabaseCleaner cleans my development database instead of test #523

Closed
RTJ opened this issue Mar 28, 2018 · 9 comments
Closed

DatabaseCleaner cleans my development database instead of test #523

RTJ opened this issue Mar 28, 2018 · 9 comments

Comments

@RTJ
Copy link

RTJ commented Mar 28, 2018

Hi.
DatabaseCleaner cleans my development database instead of test
I have tried all possible configurations.
And I still have this problem.

database_cleaner 1.6.2
Rails: 5.1.5
Ruby 2.5

Gemfile:


group :development, :test do
  gem "byebug", platforms: [:mri, :mingw, :x64_mingw]
  gem "factory_bot_rails", "~> 4.8", ">= 4.8.2"
  gem "figaro"
  gem "faker", "~> 1.8", ">= 1.8.4"
end

group :development do
  gem "web-console", ">= 3.3.0"
  gem "listen", ">= 3.0.5", "< 3.2"
  gem "spring"
  gem "spring-watcher-listen", "~> 2.0.0"
  gem "pry-rails", "~> 0.3.6"
  gem "better_errors"
  gem "binding_of_caller"
end

group :test do
  gem "capybara"
  gem "selenium-webdriver"
  gem "simplecov", "~> 0.15.1", :require => false
  gem "shoulda-matchers", "~> 3.1", ">= 3.1.2"
  gem "database_cleaner"
  gem "rspec-rails", "~> 3.7", ">= 3.7.2"
end

in rails_helper i have forced env to test:
ENV['RAILS_ENV'] = "test"

DatabaseCLeaner conf file ( i have tried all posible combinations for configuration:

RSpec.configure do |config|

  config.before(:suite) do
    DatabaseCleaner.clean_with(:truncation)
  end
  config.before(:each) do
    DatabaseCleaner.strategy = :transaction
  end
  config.before(:each, js: true) do
    DatabaseCleaner.strategy = :truncation
  end
  config.before(:each) do
    DatabaseCleaner.start
  end
  config.after(:each) do
    DatabaseCleaner.clean
  end

end

From tests when i print ENV i get TEST env but each time after test my development database is cleaned and test database is not used:
p ENV["RAILS_ENV"] #=> "test"

database config file have default configuration for test:

test:
  <<: *default
  database: appname_test
  username: <%= ENV["DATABASE_USER"] %>
  password: <%= ENV["DATABASE_PASSWORD"] %>
  reconnect: true

Any idea where to look to fix this bug ?

**after executing the rspec command all the data in my development database is deleted **

Thx

@starrychloe
Copy link

Set your environment from the shell, not from Ruby.

export RAILS_ENV=test

If you set it from inside a Ruby file, then Rails.env may have already been set.

@RTJ
Copy link
Author

RTJ commented Apr 3, 2018

I'm not sure if you have read what I have written.
If I check the environment from the test, the environment is TEST not dev, even though database_cleaner uses the development database

code inside some test:
p ENV["RAILS_ENV"] #=> "test"
how you cans see everything is correct.

database_cleaner is present only in the test environment:

group :test do
  gem "capybara"
  gem "selenium-webdriver"
  gem "simplecov", "~> 0.15.1", :require => false
  gem "shoulda-matchers", "~> 3.1", ">= 3.1.2"
  gem "database_cleaner"
  gem "rspec-rails", "~> 3.7", ">= 3.7.2"
end

@etagwerker
Copy link
Member

@RTJ Could you post a bit more information about your environment? Please share keys/values without exposing any sensitive information. We've seen unexpected behavior like this when ENV['DATABASE_URL'] is set.

@RTJ
Copy link
Author

RTJ commented Apr 9, 2018

There was some bug, this problem was present in different devices.
Test ENV was configured correctly, from tests i was geting TEST env printing the ENV in console ( p ENV["RAILS_ENV"] #=> "test" )but only database cleaner was using development database instead of test.

For now i fixed it adding ENV['RAILS_ENV'] = 'test' to spec_helper.rb ( i had it only in rails_helper.rb but it seems that it is not enough for database_cleaner )
in my case database cleaner is using test db only if i add ENV['RAILS_ENV'] = 'test' in both files (rails_helper.rb and spec_helper.rb)

@etagwerker
Copy link
Member

@RTJ Thanks for sharing! I'm glad it's solved.

@benoror
Copy link

benoror commented Jun 20, 2019

@RTJ @etagwerker Having the exact same issue! wondering how does your spec_helper.rb and rails_helper.rb files looks like, as mine starts with:

rails_helper.rb:

require 'spec_helper'
ENV['RAILS_ENV'] ||= 'test'

So, why the need to add it to spec_helper.rb? And more importantly, if ENV is set after it's required in rails_helper.rb, why is it not working?

My hunch is that this can really be a bug. Thoughts?

@dfabreguette
Copy link

Having the same issue with Rails 6 - using v1.8.5.
Adding "ENV['RAILS_ENV'] ||= 'test'" to my rails_helper file didn't fix it for me.
Any idea ?

@takamario
Copy link

#668 (comment)

↑ My this comment means the same thing, 77dece1 made Rails.env.test? return false, so development DB will be clean up after running rspec.

@rs-vinhdt
Copy link

rs-vinhdt commented Jul 12, 2024

@dfabreguette

Having the same issue with Rails 6 - using v1.8.5. Adding "ENV['RAILS_ENV'] ||= 'test'" to my rails_helper file didn't fix it for me. Any idea ?

Perhaps when you run your RSpec, you are not specifying the RAILS_ENV variable. so, ENV['RAILS_ENV'] == 'development' as default, which is why setting ENV['RAILS_ENV'] ||= 'test' is ineffective. Just change this to ENV['RAILS_ENV'] = 'test', and it should function correctly.

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

No branches or pull requests

7 participants