2011-05-18 58 views
0

我通過命令行在Rails 3.0.7(Ruby 1.8.7)項目上運行「rake測試」。僅在測試環境中使用Rake測試失敗

我的Rails應用程序是一個相當基礎的開箱即用應用程序,因爲我仍在學習。例如。我的Gemfile是:

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

當我這樣做而沒有指定環境,它的工作原理。我認爲這正在開發中,因爲當我嘗試指向開發環境時,它仍然有效!當我運行「rake test:units」或「rake test:functionals」時,這些工作也沒有錯誤。

但是,當我針對測試環境運行時,單元測試無法運行?我已經從Rubymine IDE和命令行嘗試了這一結果。

跟蹤輸出如下所示 - 先簡單的「耙測試」:

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,以便填充一些數據庫表,儘管測試數據通常是通過燈具加載的。

我想這裏的問題是試圖對測試環境運行單元測試,但我不明白爲什麼?

+1

如何運行'rake test:units'?我不認爲用任何選項「rake test」會在正常情況下的測試以外的任何環境下運行它。 (編寫一個打印Rails.env的測試,運行'RAILS_ENV = development rake test'並查看打印結果。) – jdeseno 2011-05-18 02:46:40

+0

好點,忘了說個別rake測試:units和test:functionals命令沒有錯誤地工作。現在編輯上面的聲明。 – Phantomwhale 2011-05-18 02:57:30

回答

1

當您運行rake test時,所有的單元,功能和集成測試都在測試環境中運行。從你所展示的內容來看,這些測試正如預期的那樣工作。您不需要明確告訴rake使用測試環境 - 這是假設的。

+0

好吧,我想我很樂意不指定環境,如果這是造成問題;但仍然困惑爲什麼明確地針對(默認)測試環境導致命令失敗? – Phantomwhale 2011-05-19 04:19:14

+0

當您運行「rake測試」時,首先吹掉測試環境的數據庫結構,並將其替換爲當前環境的結構(默認開發)。當你運行「rake RAILS_ENV = test test」時,你告訴它用測試數據庫結構替換測試數據庫結構,這是一個未經過測試的場景(我想不出用例)。 – 2011-05-19 15:04:14

+0

啊,我現在明白了。很高興接受我所做的有點愚蠢,但想明白它究竟是多麼的愚蠢:) – Phantomwhale 2011-05-20 06:21:40