使用 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 时,我收到预期错误包括“不能为空”,收到错误:名称警报名称不能为空。 (无)
我不明白。为什么? 谢谢!
Here's my test.
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
Here's my model.
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
And I get Expected errors to include "can't be blank" when name is set to nil, got errors: name Alert name can't be blank. (nil)
I don't get it . Why?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧好吧。如果我在模型内的验证中注释掉 :message 部分,则测试脚本将运行!现在,如果有人能解释为什么会出现这种情况,那就太好了。有可能是一个bug吗?
Well well. If I comment out the :message part in my validations inside my model the test script RUNS ! Now, if somebody could explain why that is the case , that would be great. Any chance its a shoulda bug?
如果您更改消息,则必须告知 Shoulda:
If you change the message, you have to tell Shoulda about it: