通过 rake 进行 Rails 测试:不要碰我的开发数据库

发布于 2024-12-04 19:30:34 字数 788 浏览 0 评论 0原文

我想让“rake test:units”在我的开发数据库关闭时在准备好的测试数据库上成功运行测试。目前rails正在尝试从开发数据库构建测试数据库。这可能吗?

我的生产/开发数据库脱离主机,但出于单元测试的目的,我在内存数据库中使用 sqlite。

编辑我的确切操作: 注意我正在使用 Rails 2.3

我的测试数据库在 config/database.yml 中的设置如下:

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

当我运行 rake db:test 时:load 测试数据库(只是一个文件)在rails-root 目录中正确生成。当我运行 rake test:units 时,一切正常。

现在,如果我编辑 database.yml 将我的 DEV 数据库 用户名设置为错误的内容(例如“sdlkfj”),rake test:units 会立即失败抱怨:

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

当我“真正”运行此构建时,我在一个系统中运行它,在构建过程中不允许构建进行对话。因此,这种与开发数据库对话并立即发出嘎嘎声的尝试让我丧命,而且似乎是不正确的。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

断念 2024-12-11 19:30:34

尝试在显式环境中运行 rake 任务:

rake test:units RAILS_ENV=test

根据我的经验,如果您未指定环境,则假定为开发。虽然测试数据库仍然将夹具数据插入其中,但由于某种原因,来自开发环境的内容仍然被引用。

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.

绻影浮沉 2024-12-11 19:30:34

失败的原因是“rake test:units”尝试首先确保测试数据库设置正确。为了做到这一点,它调用“rake db:test:prepare”,它从开发数据库复制当前模式(我想这是为了确保在运行测试之前所有迁移都已运行)。

我想你可以通过几种方式解决这个问题,其中一种是重写 rake 任务“db:test:prepare”,这是在 在 stackoverflow 上发布

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文