【发布时间】:2009-09-26 15:53:45
【问题描述】:
我正在寻找干净和简短的代码来测试 Rails 单元测试中的验证。
目前我正在做这样的事情
test "create thing without name" do
assert_raise ActiveRecord::RecordInvalid do
Thing.create! :param1 => "Something", :param2 => 123
end
end
我想还有更好的方法来显示验证消息吗?
解决方案:
我目前没有额外框架的解决方案是:
test "create thing without name" do
thing = Thing.new :param1 => "Something", :param2 => 123
assert thing.invalid?
assert thing.errors.on(:name).any?
end
【问题讨论】:
-
感谢您的回复。我会尝试 rspec 和其他一些时间。现在我用 assert(record.invalid?) 和 assert_equal([], record.errors.full_messages) 来帮助自己
标签: ruby-on-rails unit-testing validation