1

I am working on a Ruby on Rails application. I recently updated to the last Rails version (4.2.3) and I discovered when I run rake test it erase my development database. I am using the Figaro gem to declare all my environment variables.

Here is my application.yml file:

#
# == Database
#
db_host    : 'localhost'
db_username: 'root'
db_password: 'root'

development:
  db_name: 'database-dev' # Not found by Figaro
test:
  db_name: 'database-test' # Not found by Figaro

my database.yml file:

default: &default
  adapter: mysql2
  pool: 5
  timeout: 5000
  username: <%= Figaro.env.db_username %>
  password: <%= Figaro.env.db_password %>
  database: <%= Figaro.env.db_name %>

  development:
    <<: *default

  test:
    <<: *default

If I put db_name under the development key, I get the message no database selected but if I move it to the upper level it works. However rake test doesn't use the test key either to load the database configuration and erase my development datas.

It was working perfectly before, I don't understand what is wrong. Thanks for your help.

My project:

  • Rails 4.2.3
  • Ruby 2.2.2
  • Figaro 1.1.1
zero323
  • 322,348
  • 103
  • 959
  • 935
anthony
  • 640
  • 1
  • 10
  • 32

1 Answers1

0

Could you show how you're running the test spec? What environments do you have set-up (in config/environments dir)? Also, what version of RSpec do you run (and do you have rspec-rails gem installed)?

Your spec_helper.rb file (or with RSpec 3.x rails_helper.rb) should have this line at the top:

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

If not, trying adding it. Also, you can try to add a print statement for debugging:

puts ENV['RAILS_ENV']

In general, this should always work, but it's a ugly (and you shouldn't have to do it):

RAILS_ENV=test bundle exec rspec spec
matb
  • 851
  • 13
  • 23
  • Thanks for your reply. I am not using rspec, but the default one from Rails. ENV['RAILS_ENV'] ||= 'test' is properly setup in my test_helper.rb – anthony Jul 21 '15 at 09:46
  • I see, so you use Minitest then. If you go to `test/test_helper.rb` you should see the same line. First of all, you should check to make sure that you have two databases created (`mysql -u root`, and then in the console `SHOW DATABASES;`). Normally, when you run `rake db:setup`, the command is executed in *development* env, so (development will be used for creating your test db). You should use `RAILS_ENV=test rake db:setup`. – matb Jul 21 '15 at 17:31