开发者

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

开发者 https://www.devze.com 2023-04-05 07:16 出处:网络
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 developmen

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开发者_如何学JAVA 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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消