Rake测试仅在测试环境中失败

时间:2011-05-18 01:59:58

标签: ruby-on-rails testing rake

我通过命令行在我的Rails 3.0.7(Ruby 1.8.7)项目上运行“rake test”。

我的rails应用程序是一个相当基本的开箱即用应用程序,因为我还在学习。例如。我的Gemfile是:

source 'http://rubygems.org'
gem 'rails', '3.0.7'
gem 'sqlite3'

当我这样做而没有指定环境时,它可以工作。我认为这是在进行开发,因为当我尝试并指向开发环境时,它仍然有效!此外,当我运行“rake test:units”或“rake test:functionals”时,这些工作也没有错误。

但是,当我在测试环境中运行时,单元测试无法运行?我从Rubymine IDE和命令行都尝试过这个结果。

跟踪输出如下所示 - 首先是普通的“rake test”:

C:\Users\Ben\dev\railstest>rake test --trace
(in C:/Users/Ben/dev/railstest)
** Invoke test (first_time)
** Execute test
** Invoke test:units (first_time)
** Invoke test:prepare (first_time)
** Invoke db:test:prepare (first_time)
** Invoke db:abort_if_pending_migrations (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute db:abort_if_pending_migrations
** Execute db:test:prepare
** Invoke db:test:load (first_time)
** Invoke db:test:purge (first_time)
** Invoke environment
** Execute db:test:purge
** Execute db:test:load
** Invoke db:schema:load (first_time)
** Invoke environment
** Execute db:schema:load
** Execute test:prepare
** Execute test:units
Loaded suite C:/dev/lang/Ruby187/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake/rake_test_loader Started
.......................
Finished in 2.821289 seconds.

23 tests, 24 assertions, 0 failures, 0 errors
** Invoke test:functionals (first_time)
** Invoke test:prepare
** Execute test:functionals
Loaded suite C:/dev/lang/Ruby187/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake/rake_test_loader Started
.............
Finished in 3.34668 seconds.

13 tests, 25 assertions, 0 failures, 0 errors
** Invoke test:integration (first_time)
** Invoke test:prepare
** Execute test:integration

现在针对测试环境的失败运行

C:\Users\Ben\dev\railstest>rake environment RAILS_ENV=test test --trace
(in C:/Users/Ben/dev/railstest)
** Invoke environment (first_time)
** Execute environment
** Invoke test (first_time)
** Execute test
** Invoke test:units (first_time)
** Invoke test:prepare (first_time)
** Invoke db:test:prepare (first_time)
** Invoke db:abort_if_pending_migrations (first_time)
** Invoke environment
** Execute db:abort_if_pending_migrations
** Execute db:test:prepare
** Invoke db:test:load (first_time)
** Invoke db:test:purge (first_time)
** Invoke environment
** Execute db:test:purge
** Invoke test:functionals (first_time)
** Invoke test:prepare
** Execute test:functionals
Loaded suite C:/dev/lang/Ruby187/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake/rake_test_loader Started
.............
Finished in 3.328125 seconds.

13 tests, 25 assertions, 0 failures, 0 errors
** Invoke test:integration (first_time)
** Invoke test:prepare
** Execute test:integration
Errors running test:units!

错误消息中没有更多文本,也没有进一步指示错误实际是什么。我已经在sqlite3环境和db:schema:load上重新运行db:seed以填充一些数据库表,尽管测试数据通常通过夹具加载。

我认为这里的问题是尝试针对测试环境运行单元测试,但我不明白为什么?

1 个答案:

答案 0 :(得分:1)

运行rake test时,所有单元,功能和集成测试都在测试环境中运行。根据您的显示,测试完全按预期工作。您不需要明确告诉rake使用测试环境 - 假设。