应该:测试 validates_presence_of :on =>:更新 [英] Shoulda: Test validates_presence_of :on => :update
问题描述
我在我从事的一个项目中结合使用了 Shoulda 和 Test::Unit.我遇到的问题是我最近改变了这个:
I'm using Shoulda in combination with Test::Unit on one of the projects I work on. The issue I'm running into is that I recently changed this:
class MyModel < ActiveRecord::Base
validates_presence_of :attribute_one, :attribute_two
end
为此:
class MyModel < ActiveRecord::Base
validates_presence_of :attribute_one
validates_presence_of :attribute_two, :on => :update
end
以前,我的(通过)测试是这样的:
Previously, my (passing) tests looked like this:
class MyModelTest < ActiveSupport::TestCase
should_validate_presence_of :attribute_one, :attribute_two
end
据我所知,should_validate_presence_of
没有参数会导致此测试继续通过上面指定的更改.在测试:attribute_two
的需求时,没有放弃应该,有什么办法可以解决这个问题吗?
As far as I can tell, there is no parameter to should_validate_presence_of
that will cause this test to continue to pass with the changes specified above. Short of abandoning Shoulda when testing the requirement of :attribute_two
, is there any way around this?
推荐答案
过去我只是使用了一个小的自定义应该块来解决这个问题:
In that past I have just used a small custom should block to get around this problem:
should "require :attr_two on update" do
mm = Factory(:my_model)
mm.attr_two = nil
mm.save
assert_equal false, mm.valid?
assert_equal("can't be blank", mm.errors.on(:attr_two))
end
希望将来通过允许进一步的 AR 验证选项来不断改进.让我知道你的想法,干杯.
Hopefully shoulda will keep improving by allowing further AR validation options in the future. Let me know what you think, cheers.
这篇关于应该:测试 validates_presence_of :on =>:更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!