验证属性的存在失败

时间:2015-02-04 21:15:48

标签: rspec2

在我的班级Foo中,我有:

belongs_to :assessment_type
validate :assessment_type, presence: true

我的RSpec测试有:

  it { is_expected.to validate_presence_of(:assessment_type) }

测试因此错误而失败:

1) Foo should require assessment_type to be set
 Failure/Error: it { is_expected.to validate_presence_of(:assessment_type) }
   Expected errors to include "can't be blank" when assessment_type is set to nil, got errors: ["can't be blank (attribute: \"name\", value: nil)", "can't be blank (attribute: \"description\", value: nil)", "can't be blank (attribute: \"logo\", value: nil)", "can't be blank (attribute: \"url_code\", value: nil)", "can't be blank (attribute: \"starts_at\", value: nil)", "can't be blank (attribute: \"contract\", value: nil)", "can't be blank (attribute: \"organization\", value: nil)"]

错误字符串(名称,描述,...)中列出的所有属性与assessment_type具有相同的validate语句。

为什么会失败?

1 个答案:

答案 0 :(得分:1)

我发现了问题。这一行:

validate :assessment_type, presence: true

应该是

validates :assessment_type, presence: true