有没有一种好方法可以在 Rails 中使用 `:on` 参数测试 `before_validation` 回调?
我有一个 before_validation :do_something, :on =>; :create
在我的模型之一中。
我想测试这种情况是否发生,并且不会发生在 :save
上。
有没有一种简洁的方法来测试这个(使用Rails 3,Mocha和Shoulda),而不需要做类似的事情:
context 'A new User' do
# Setup, name test etc
@user.expects(:do_something)
@user.valid?
end
context 'An existing User' do
# Setup, name test etc
@user.expects(:do_something).never
@user.valid?
end
在shoulda API中找不到任何东西,这感觉相当不干……
有什么想法吗?谢谢 :)
I have a before_validation :do_something, :on => :create
in one of my models.
I want to test that this happens, and doesn't happen on :save
.
Is there a succinct way to test this (using Rails 3, Mocha and Shoulda), without doing something like:
context 'A new User' do
# Setup, name test etc
@user.expects(:do_something)
@user.valid?
end
context 'An existing User' do
# Setup, name test etc
@user.expects(:do_something).never
@user.valid?
end
Can't find anything in the shoulda API, and this feels rather un-DRY...
Any ideas? Thanks :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为你需要改变你的方法。您正在测试 Rails 是否正常工作,而不是您的代码是否适用于这些测试。考虑测试您的代码。
例如,如果我有一个相当愚蠢的类:
我实际上会像这样测试它:
I think you need to change your approach. You are testing that Rails is working, not that your code works with these tests. Think about testing your code instead.
For example, if I had this rather inane class:
I would actually test it like this:
您可能喜欢 shoulda 回调匹配器。
You might like the shoulda callback matchers.