rails-rspec期望自定义验证失败



我在使用自定义验证器运行rspec预期时遇到此错误。

expect {@ua.save!}.to  raise_error(ActiveRecord::RecordInvalid,'Validation failed: This question is no longer active')

故障

expected ActiveRecord::RecordInvalid with "Validation failed: This question is no longer active", got #<ActiveRecord::RecordInvalid: Validation failed: This question is no longer active.> with backtrace:

这似乎只是我的自定义验证的问题。参见此型号:

class UserAnswer < ActiveRecord::Base
  belongs_to :user
  belongs_to :question
  validate :questionIsActive?
  private
  def questionIsActive?
    errors.add(:base, "This question is no longer active.") if !self.question.is_active?
  end
end

使用:轨道3.2.11Rspec导轨2.12.2

您有一个打字错误。在字符串中添加句点:

expect {@ua.save!}.to  raise_error(ActiveRecord::RecordInvalid,'Validation failed: This question is no longer active.')

注意:您目前的预期是:

"验证失败:此问题不再有效"

但需要:

'验证失败:此问题不再有效。'

以便它与您的验证字符串匹配:

"此问题不再有效。"

相关内容

  • 没有找到相关文章

最新更新