使用Shoulda测试验证的问题

时间:2010-08-31 08:57:38

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

这是我的测试。

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时,我得到预期错误以包含“不能为空”,得到错误:名称警报名称不能为空。 (无)

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

2 个答案:

答案 0 :(得分:1)

好吧。如果我在模型中的我的验证中注释掉:消息部分测试脚本RUNS!现在,如果有人可以解释为什么会这样,那就太好了。任何机会它都应该是一个bug?

答案 1 :(得分:1)

如果您更改了消息,则必须告诉Shoulda:

should_validate_presence_of(:name).with_message(/can.t be blank/)
相关问题