4

我正在使用 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 为 nil 时,您会期望验证中出现错误,即 slug“不能为空”。

如果我添加验证,测试通过,但我希望首先看到它正确失败!

另外,为什么第一次测试通过了?我的页面模型不包含belongs_to关联!

测试很烂!:-(

4

1 回答 1

1

这是旧版本中的一个错误shoulda。您可以查看此线程以获取更多信息。

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

于 2013-06-11T23:23:10.723 回答