应该:测试 validates_presence_of :on => :更新
我在我从事的一个项目中将 Shoulda 与 Test::Unit 结合使用。我遇到的问题是我最近将其更改为:
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
,我的(通过)测试看起来像这样:
class MyModelTest < ActiveSupport::TestCase
should_validate_presence_of :attribute_one, :attribute_two
end
据我所知, should_validate_presence_of
没有参数可以使此测试继续通过上面指定的更改。在测试 :attribute_two
的要求时,没有放弃 Shoulda,有什么办法可以解决这个问题吗?
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
to this:
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
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
像这样的事情怎么办? (对于
shoulda-matchers-3.1.1
)What about something like this? (for
shoulda-matchers-3.1.1
)过去,我只是使用了一个小的自定义 should 块来解决这个问题:
希望 shoulda 将来能够通过允许进一步的 AR 验证选项来不断改进。让我知道你的想法,干杯。
In that past I have just used a small custom should block to get around this problem:
Hopefully shoulda will keep improving by allowing further AR validation options in the future. Let me know what you think, cheers.
在 Rspec 中您可以执行以下操作:
In Rspec you can do the following:
我尝试了类似于 tsdbrown 建议的解决方案。当我有以下情况时,这种类型的测试会通过:
但是如果我将模型更改为:
则测试会失败:
它会失败,因为 :attr_two 错误是 [] 而不是 ["can't be Blank"]
I have tried a solution similar to what tsdbrown suggested. This type of test passes when I have:
But the test fails if I change the model to:
It fails because the :attr_two error is [] instead of ["can't be blank"]