gpt4 book ai didi

ruby-on-rails - 为什么这个测试结果是倒退的?

转载 作者:行者123 更新时间:2023-12-04 06:27:56 25 4
gpt4 key购买 nike

我正在使用 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协会!

测试糟透了! :-(

最佳答案

这是旧版本 shoulda 中的一个错误.你可以看到this thread了解更多信息。

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

关于ruby-on-rails - 为什么这个测试结果是倒退的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16384463/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com