这看起来有点傻,但我刚刚开始使用Rspec,遵循Everyday Rails Testing with RSpec教程。
我有一个Review
模型,它有一个布尔列recommended
:
Class Review < ActiveRecord::Base
validates_inclusion_of :recommended, :in => [true, false], message: "recommended not a boolean value"
end
所以我写了一个基本的Rspec测试,以确保用户无法以零值保存:
it "is invalid without a non-boolean recommended value" do
review = Review.new(recommended: nil)
review.valid?
expect(review.errors[:recommended]).to include("not a boolean value")
end
然而,Rspec未通过测试。终端说:
1) Review is invalid without a non-boolean recommended value
Failure/Error: expect(review.errors[:recommended]).to include("not a boolean value")
expected ["recommended not a boolean value"] to include "not a boolean value"
# ./spec/models/destination_review_spec.rb:36:in `block (2 levels) in <top (required)>'
第二行让我感到困惑:expected ["recommended not a boolean value"] to include "not a boolean value"
,因为子字符串包含在错误消息中。我写过的其他测试都没有收到此错误。
有人可以向我解释为什么会失败吗?
答案 0 :(得分:0)
to include
测试集合中的成员资格或匹配字符串中的子字符串。你试图混合这两者。即review.errors[:recommended]
返回一个数组,并且您正在尝试检查字符串。如果你要做review.errors[:recommended].join
它应该通过。