我测试我的对象是否使用assert_not
验证失败,因此它实际上传递了失败的验证。但是,我想知道具体哪些验证失败了。
基本上,在我的情况下,如何确保我的对象至少失败validates_acceptance_of :terms
?
class StaffTest < ActiveSupport::TestCase
context "Save staff and validate" do
setup do
@staff = Fabricate.build(:staff)
@new_staff = Staff.new
end
teardown do
@staff = nil
@new_staff = nil
end
should "fail if save without accepting terms" do
@staff.validate_terms = true
assert_not @staff.valid?, @staff.errors.full_messages.inspect
end
end
end
据说,我希望我的测试通过,因为它无法通过密码验证。然而,实际上它确实通过了,但它通过了因为它在条款验收验证上失败了。在这种情况下,我的测试没有达到目的。因此,我想确保测试传递正确的失败。
我正在使用Rails 4,Shoulda和ActiveSupport :: TestCase(Minitest)
答案 0 :(得分:2)
@ staff.errors.messages将包含每个错误字段名称和相应消息的哈希值。你可以用它来达到这个目的。
assert !@staff.save
assert @staff.errors.messages.values.include?("msg must be there")