2010-08-31 64 views
1

这是我的测试。使用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 

我也得到预期的错误,包括时名称设置为无“不能为空”,得到了错误:命名警报名称不能为空。 (无)

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

回答

1

好吧。如果我在模型中的验证中注释掉消息部分,测试脚本RUNS!现在,如果有人能解释为什么会这样,那会很好。任何机会它应该错误?

1

如果你改变了消息,你必须告诉早该一下:

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

我认为应该是(后应该用空格)“应validate_presence_of”,而不是“should_validate_presence_of” – 2013-05-07 18:46:25

相关问题