gpt4 book ai didi

ruby-on-rails - rails rspec 预计自定义验证失败

转载 作者:行者123 更新时间:2023-12-04 17:23:47 24 4
gpt4 key购买 nike

使用自定义验证器运行我的 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

使用:
rails 3.2.11
Rspec-rails 2.12.2

最佳答案

你有一个错字。在字符串中添加句点:

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

注意:您目前在您的期望中:

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

但需要:

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

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

“这个问题不再有效。”

关于ruby-on-rails - rails rspec 预计自定义验证失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14713936/

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