【问题标题】:Problem with testing validations using Shoulda使用 Shoulda 测试验证的问题
【发布时间】:2010-08-31 08:57:38
【问题描述】:

这是我的测试。

class AlertSettingTest < ActiveSupport::TestCase
  context "For ALL Alert Settings" do
    setup do
  @alert = AlertSetting.create({ :alert_type_id=>'1',:name => 'xxx'})
  end
  subject { @alert }
  should_belong_to :alert_type
  should_validate_presence_of :name
  end
end

这是我的模型。

class AlertSetting < ActiveRecord::Base
  belongs_to :alert_type

  validates_presence_of :name, :message => "Alert name can't be blank."
  validates_presence_of :alert_type_id, :message => "Please select valid Alert Type."
 end

当名称设置为 nil 时,我收到包含“不能为空”的预期错误,出现错误:名称警报名称不能为空。 (无)

我不明白。为什么? 谢谢!

【问题讨论】:

    标签: ruby-on-rails unit-testing shoulda


    【解决方案1】:

    好吧好吧。如果我在模型内部的验证中注释掉 :message 部分,测试脚本就会运行!现在,如果有人能解释为什么会这样,那就太好了。有没有可能是一个错误?

    【讨论】:

      【解决方案2】:

      如果你改变了消息,你必须告诉Shouda:

      should_validate_presence_of(:name).with_message(/can.t be blank/)
      

      【讨论】:

      • 我认为应该是“should validate_presence_of”(should 后面有一个空格),而不是“should_validate_presence_of”
      猜你喜欢
      • 2014-04-05
      • 2013-09-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多