我正在使用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"不能为空白"的错误。
如果我添加验证,测试就通过了,但我希望首先看到它正确失败!
还有,为什么第一个测试通过了?我的Pages模型不包含belongs_to
关联!
测试很糟糕!: - (
这是旧版shoulda
中的一个错误。你可以看到这个线程的更多信息。
当我更新到最新版本(现在是2.2.0)时,我看到了正确的行为和消息,如Expected errors to include...
。