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

发布于 2024-11-08 17:43:23 字数 2921 浏览 3 评论 0原文

我正在通过命令行在我的 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:functions”时,这些也可以正常工作。

但是,当我在测试环境中运行时,单元测试无法运行?我已经从 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 来填充一些数据库表,尽管测试数据通常是通过固定装置加载的。

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

I am running "rake test" over my Rails 3.0.7 (Ruby 1.8.7) project via the command line.

My rails app is a fairly basic out-the-box app, as I am still learning. E.g. my Gemfile is:

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

When I do this without specifying the environment, it works. I presume this is hitting development, as when I try and point at the development environment, it still works ! Also when I run "rake test:units" or "rake test:functionals" these work without error too.

However, when I run against the test environment, the unit tests fail to run ? I have tried this both from Rubymine IDE and command line with the same results.

The trace outputs look as follows - firstly plain "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

And now the failing run against the test environment

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!

There is no more text in the error message, and no further indication what the errors actually are. I have re-run db:schema:load on both sqlite3 environments and db:seed to populate a few database tables too, although the test data is loaded via fixtures normally.

I figure the problem here is trying to run unit tests against the test environment, but I can't understand why ?

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

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

发布评论

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

评论(1

夜夜流光相皎洁 2024-11-15 17:43:23

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

When you run rake test all unit, functional, and integration tests are run in the test environment. From what you've shown, the tests are working exactly as expected. You don't need to explicitly tell rake to use the test environment - that is assumed.

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