【问题标题】:Rake test failing in test environment only仅在测试环境中 Rake 测试失败
【发布时间】: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


【解决方案1】:

当您运行rake test 时,所有单元、功能和集成测试都在测试环境中运行。从您所展示的内容来看,测试完全按预期工作。您不需要明确告诉 rake 使用测试环境 - 这是假设的。

【讨论】:

  • 好吧,我想我很高兴不指定环境,如果这是导致问题的原因;但仍然感到困惑,为什么明确针对(默认)测试环境会导致命令失败?
  • 当您运行“rake test”时,您首先要清除测试环境的数据库结构,并将其替换为当前环境的结构(默认为开发)。当您运行“rake RAILS_ENV=test test”时,您是在告诉它用测试数据库结构替换测试数据库结构,这是一个尚未测试的场景(我想不出用例)。跨度>
  • 啊,我现在明白了。很高兴接受我所做的有点愚蠢,但想了解它到底有多愚蠢:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-25
  • 1970-01-01
  • 2016-09-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多