使用“rake test”时出现问题
我想知道如何在我的 Rails 应用程序中设置测试。当我运行 rake test 时,首先奇怪的是,它针对我的开发启动了一堆 CREATE TABLE 。数据库(嗯..不喜欢这个..)。因此,我启动了rake test RAILS_ENV=test
,甚至尝试bundle exec rake test RAILS_ENV=test
。现在,CREATE TABLE
针对我的测试数据库,但全部失败并出现此错误:
** Execute test:units
test/unit/category_test.rb:5:in `test': unknown command 't' (ArgumentError)
from test/unit/category_test.rb:5:in `<class:CategoryTest>'
我在 Rails 3 中使用了基本生成器,并且没有更改任何内容。所以我在caterogy_test.rb中有这个:
require 'test_helper'
class CategoryTest < ActiveSupport::TestCase
# Replace this with your real tests.
test "the truth" do
assert true
end
end
我使用Rails 3.0.7和基本配置。
有什么想法吗?
编辑
我变得疯狂,做了很多尝试,似乎都不起作用。当我启动一个包含一些内容的新应用程序时,rake test
工作正常,但是当我在当前的应用程序上尝试此操作时,它总是针对我的开发人员启动。 db 并且根本不工作。我尝试编辑测试文件,将它们恢复回来,尝试用不同的方式删除/设置测试数据库,尝试不同的rake版本,一方面比较我当前的应用程序,另一方面比较一个全新的应用程序。 ..什么也没找到..救命!
编辑 2
听起来很蹩脚,但是 rake
与 rake test
做同样的事情正常吗?
编辑3
听起来很奇怪,当我继续解决问题时,我意识到每次运行rake test
时,它都会在开发环境而不是测试环境中执行操作(查看日志)。它在我的计算机 OSX 和我们的服务器 FreeBSD 上为所有 Rails 3.0.7 应用程序执行此操作。您确定默认情况下rake test
应该在测试环境中工作吗?
编辑4
请帮忙!
编辑 5 - 摘要
当在我的计算机或 Rails 3.0.7 中的服务器上使用不同的应用程序运行 rake test
时,它会执行以下操作:
- 运行
CREATE TABLE
和INSERT针对开发人员的 INTO 迁移。数据库。
- 不要清空开发人员。数据库。
- development.log 被写入而不是 test.log,
- 这也是一个特定应用程序出现错误
unknown comman 't'
的问题。
编辑 6 - 数据库配置
与默认值相比没有任何变化: https://gist.github.com/1006199
编辑7
rake db:test:prepare --trace
->没有任何中断(但继续打印(first_time) https://gist.github.com/1007340
与 RAILS_ENV="test"
对于 rake 来说,一切都很顺利。它写在测试日志上。
ruby -I test test/unit/category_test.rb 与 rake 相同的错误,但没有在开发上写入。或测试日志。
I wonder how to setup testing in my rails apps. When I run rake test
, first thing odd, it launch a bunch of CREATE TABLE
against my dev. database (hum.. do not like this..). So I launch rake test RAILS_ENV=test
and I even try bundle exec rake test RAILS_ENV=test
. Now, the CREATE TABLE
is against my test database but all fails with this error :
** Execute test:units
test/unit/category_test.rb:5:in `test': unknown command 't' (ArgumentError)
from test/unit/category_test.rb:5:in `<class:CategoryTest>'
I have used basic generator in Rails 3 and do not change anything. So I have this in caterogy_test.rb :
require 'test_helper'
class CategoryTest < ActiveSupport::TestCase
# Replace this with your real tests.
test "the truth" do
assert true
end
end
I use Rails 3.0.7 and basic config.
Any ideas ?
EDIT
I am becoming crazy, made a lot of tries, neither seems to work. When I start a new application with a few things, rake test
works fine but when I try this on my current one, it launch always against my dev. db and do not work at all. I have tried to edit the test files, to revert them back, try to remove/setup test db with different ways, try different rake version, compare a lot of things on one side my current application and on the other a brand new one... Found nothing.. Help !
EDIT 2
Sounds lame, but is it normal that rake
does the same thing than rake test
?
EDIT 3
Sounds odds, while I continue to work on what's wrong, I realize that every-time I run rake test
, it does stuff on the dev environment and not the test one (watching the logs). It does this on my computer OSX and on our server FreeBSD for all the Rails 3.0.7 apps. Are you sure rake test
is supposed to work on the test environment by default ?
EDIT 4
Please help!
EDIT 5 - SUMMARY
When running rake test
in my computer or on our server in Rails 3.0.7 with different apps it does the following :
- run
CREATE TABLE
andINSERT INTO
migration against the dev. db. - do not empty the dev. db.
- development.log gets written not the test.log
- also an issue with the error
unknowm comman 't'
with one specific app.
EDIT 6 - db config
Nothing change from the default yet : https://gist.github.com/1006199
EDIT 7
rake db:test:prepare --trace
-> nothing break (but keep printing (first_time)
https://gist.github.com/1007340
With RAILS_ENV="test"
for rake, everything goes fine. It write on the test logs.
ruby -I test test/unit/category_test.rb
same erros than with rake
, but no write on the dev. or test logs.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一堆无序的答案:
“CREATE TABLE”语句通常意味着您的 test_db 是从头开始创建的(默认情况下,在测试任务之前,启动 db:migrate)。你确定他们是在 dev_db 上调用的吗?
还要检查您的 config/database.yml 以查看是否存在一些拼写错误(例如:对测试和开发环境使用相同的表)
看起来您的某些迁移文件中存在错误(“t”错误记住了中的块)迁移)。
“rake test”是默认任务,这就是为什么当您只启动不带参数的“rake”时它会运行。
编辑:
根据我在编辑中看到的内容,从 5 及以上版本来看,您的环境文件似乎存在一些问题。所以尝试仔细检查:
* 配置/环境/test.rb
* 配置/应用程序.rb
* config/environment.rb
如果使用 RAILS_ENV="test",一切顺利,那么我几乎可以肯定您已经更改了应用程序中的一些默认行为(配置、环境变量、任何特定的 gem? )
另外,在您的 test/test_helper.rb 中,在文件开头添加 RAILS_ENV='test' ,这应该强制测试环境。
a bunch of unorderd answers:
the "CREATE TABLE" statements usually means that your test_db is created from scratch (by default, before test task, a db:migrate is launched). are you sure they're called on dev_db?
also check your config/database.yml to see if there's some typo (eg: using same table for test and dev environments)
it looks like there's an error in some of your migration files (that 't' error remember blocks in migrations).
"rake test" is the default task, that's why it's run when you just launch "rake" without arguments.
EDIT:
according on what I see on edits, from 5 and above, it looks like you have some issue with environment files. so try to double-check:
* config/environments/test.rb
* config/application.rb
* config/environment.rb
if with RAILS_ENV="test", everything goes fine, then I'm almost sure you have changed some default behaviour in your app (configs, env variables, any particular gem?)
also, in your test/test_helper.rb, add RAILS_ENV='test' at the beginning of file, this should force test environment.
我有同样的错误消息,除了对我来说它说:在“测试”中:未知命令“i”(ArgumentError)。
“修复”或“解决方法”是简单地使用:
而不是使用“rake test”
I had that same error message, except to me it said: in `test': unknown command 'i' (ArgumentError).
The 'fix' or 'workaround' was to simply use:
instead of using 'rake test'