【发布时间】:2011-05-18 01:59:57
【问题描述】:
我正在通过命令行对我的 Rails 3.0.7 (Ruby 1.8.7) 项目运行“rake 测试”。
我的 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:seed 上重新运行 db:schema:load 以填充一些数据库表,尽管测试数据通常是通过夹具加载的。
我认为这里的问题是尝试针对测试环境运行单元测试,但我不明白为什么?
【问题讨论】:
-
运行
rake test:units的时候怎么样?我认为rake test不会在任何环境下运行它,而不是在正常条件下进行测试。 (编写一个打印 Rails.env 的测试,运行RAILS_ENV=development rake test并查看打印出来的内容。) -
啊,好点,忘了说单独的 rake test:units 和 test:functionals 命令可以正常工作。上面编辑现在声明。
标签: ruby-on-rails testing rake