Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails testing through rake: don't touch my dev database

I want to have 'rake test:units' run the tests on a prepared test database successfully while my development database is down. Currently rails is trying to build the test database from the development database. Is this possible?

I am in a situation where my prod/dev databases are off host, but for the purpose of unit tests I am using an sqlite in memory database.

EDIT for my exact actions: Note I am using Rails 2.3

My test database is setup like this in config/database.yml:

test:
  adapter: sqlite3
  database: 'sqlite3_unittest_file.dat'
  pool: 5
  timeout: 5000

When I run rake db:test:load the test database (which is just a file) is generated correctly in the rails-root directory. And when I run rake test:units, everything works.

Now if I edit database.yml to set my DEV database username to something wrong (like 'sdlkfj'), rake test:units fails instantly complaining:

Access denied for user 'sdlkfj'@'myhostnsmae' (using password: YES)

When I'm running this build 'for real', I'm running it in a system where builds are not allowed to talk off box during the build process. Hence this attempt to talk to the dev database and instantly croaking is killing me, and seems incorrect.

like image 690
Cory Kendall Avatar asked Sep 17 '11 08:09

Cory Kendall


2 Answers

Try running the rake task with an explicit environment:

rake test:units RAILS_ENV=test

If you don't specify an environment, development is assumed, in my experience. And while the test database still gets the fixture data inserted into it, stuff from the development environment still gets referenced for some reason.

like image 153
jefflunt Avatar answered Nov 19 '22 17:11

jefflunt


The reason this fails is because 'rake test:units' trys to first ensure that the test database is setup properly. In order to do that it invokes 'rake db:test:prepare' which copies over the current schema from the dev database (this is I guess to ensure all migrations have run before you run tests).

I guess you can get around this in a few ways, one being overriding the rake task "db:test:prepare" which has been suggest in this post on stackoverflow.

like image 4
jake Avatar answered Nov 19 '22 16:11

jake