2013-05-05 31 views
4

我正在寫使用測試我的第一個測試:單元和早該,所以這可能是我的一個簡單的誤會,但由於不包含驗證一個頁面模型和測試:爲什麼這個測試結果倒退?

#test/unit/page_test.rb  
require 'test_helper' 

class PageTest < ActiveSupport::TestCase 
    should belong_to(:site) 
    should validate_presence_of(:slug) 
end 

第二測試失敗如預期(因爲沒有驗證的模型):

# Running tests: 

[2/2] PageTest#test: Page should require slug to be set. = 0.02 s 
    1) Failure: 
test: Page should require slug to be set. (PageTest) [/Users/Matt/.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/shoulda-context-1.1.1/lib/shoulda/context/context.rb:339]: 
Did not expect errors to include "can't be blank" when slug is set to nil, got error: 

Finished tests in 0.115436s, 17.3256 tests/s, 17.3256 assertions/s. 
2 tests, 2 assertions, 1 failures, 0 errors, 0 skips 

但是錯誤消息Did not expect errors to include "can't be blank" when slug is set to nil似乎倒退。不應該是這樣寫的:Expected errors to include "can't be blank",因爲如果我測試的驗證存在,你會期望從驗證中發現一個錯誤,當slug爲零時,slug「不能爲空」。

測試通過,如果我添加驗證,但我想看到它首先正確地失敗!

此外,爲什麼第一次測試通過?我的頁面模型不包含belongs_to關聯!

測試很爛! :-(

+0

應該在哪個對象上被調用嗎?不應該測試有上下文和描述嗎?'it'子句在哪裏?這是RSpec,不是嗎? – 2013-05-05 12:29:13

+0

不, Test :: Unit with shoulda - 認爲最好在學習RSpec之前學習「Rails方式」。 – Matt 2013-05-05 12:52:18

回答

1

這是在舊版本的shoulda的錯誤。你可以看到this thread獲取更多信息。

當我更新到最新版本(2.2.0現在),我看到正確的行爲和消息如Expected errors to include...

+0

感謝您的確認。我的第一個測試在shoulda中發現了這個bug,因爲它讓我脫離了這個組合,而我轉而選擇了RSpec(作爲一個測試新手而不是一個套件的奢侈品)。雖然在鏈接線程中閱讀了本應該測試覆蓋率較低的地方,但我很開心(並且有點驚訝)! ;-) – Matt 2013-06-13 00:01:31

+0

RSpec是一個測試套件,如果您願意的話,「shoulda」是它的許多「語言」之一。我聽到你說你可能會被推遲,但我已經用了很長時間,仍然衷心推薦他們兩個。 – 2013-06-13 08:33:14