danieleds
danieleds

Reputation: 668

Rails gem uses the development db instead of the testing one when executing "rake test"

I'm using a gem with my rails 3 project, "rails-settings-cached", and when I'm in development mode the gem can access the database without any problem. However, when I run "rake test", a lot of tests fails because "rails-settings-cached" keeps using the development db instead of the testing one.

The other parts of my application works fine. What should I do to connect the gem to the right database?

development:
  adapter: mysql2
  encoding: utf8
  reconnect: false
  database: mmdb_development
  pool: 5
  username: ***
  password: ***
  socket: /var/run/mysqld/mysqld.sock

test:
  adapter: mysql2
  encoding: utf8
  reconnect: false
  database: mmdb_test
  pool: 5
  username: ***
  password: ***
  socket: /var/run/mysqld/mysqld.sock

Upvotes: 1

Views: 531

Answers (3)

sameera207
sameera207

Reputation: 16629

Try explicitly set your rals env to test in test/test_helper.rb on the very top of the test_helper.rb file

ENV["RAILS_ENV"] = "test"

Upvotes: 0

tovodeverett
tovodeverett

Reputation: 1053

The problem with the caching in rails-settings-cached is that the default cache in Rails 3.2.8 (and probably in Rails 3 on) is ActiveSupport::Cache::FileStore. This caches to a file that is in a shared location and so is shared between development, test, and production. This can cause all sorts of issues!

The Caching with Rails Guide claims that the default is ActiveSupport::Cache::MemoryStore, but it appears that the documentation is incorrect. I have submitted an issue against rails regarding the apparent documentation inaccuracy.

One solution is to switch test and development to using ActiveSupport::Cache::MemoryStore. If the default of 32 megabytes is enough, you can simply add the following line to both development.rb and test.rb in config/environments (inside the .configure do block, of course):

config.cache_store = :memory_store

Upvotes: 1

danieleds
danieleds

Reputation: 668

The problem is that rails-settings-cached... well... caches the settings. So, when you run your tests, it is actually using the old cached values from the "development" database.

A possible workaround can be running rake tmp:cache:clear before and after the tests. It works, but to automate it you can put these methods inside test_helper.rb:

def setup
  Rails.cache.clear
end

def teardown
  Rails.cache.clear
end

Upvotes: 2

Related Questions