为什么这个测试结果会倒退?

时间:2013-05-05 12:20:30

标签: ruby-on-rails unit-testing shoulda

我正在使用Test:Unit和Shoulda编写我的第一个测试,所以这对我来说可能是一个简单的误解,但鉴于Pages模型不包含验证和测试:

#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关联!

测试很糟糕! : - (

1 个答案:

答案 0 :(得分:1)

这是旧版shoulda中的错误。您可以查看this thread了解详情。

当我更新到最新版本(现在为2.2.0)时,我看到了正确的行为和消息,如Expected errors to include...