问题描述
我想发现BDD缺少ActiveRecord :: Base.find方法的:include参数.因此,我的想法是在规格中添加以下内容:
I want to discover with BDD missing :include params for ActiveRecord::Base.find method. So my idea is to have in spec something like this:
ActiveRecord::Base.should_receive(:find).once.and_proxy_to_original_method
parent = SomeClass.find 34
parent.child.should be_loaded
parent.other_children.should be_loaded
如果未急切加载#child或#other_children关联,则期望将失败,并显示以下内容:预期的ActiveRecord :: Base.find将被调用一次,但通过以下参数又被调用了2次:1. ...; 2. ..."
If #child or #other_children associations are not eager loaded, expectation should fail with something like:"Expected ActiveRecord::Base.find to be invoked once but it was invoked 2 more times with following args: 1. ...; 2. ..."
有人知道是否有匹配器或类似器.
Does anyone know if there's some matcher that works like this or how to make this.
谢谢
推荐答案
我认为我遇到了相同的问题.在您的特殊情况下,我会做的,我觉得很干净.
I think I had the same problem here. In your particular case I would do this which I find quite clean.
original_method = ActiveRecord::Base.method(:find)
ActiveRecord::Base.should_receive(:find).once do (*args)
original_method.call(*args)
end
我相信您可以将Rspec Mocks::MessageExpectation
类扩展为包含and_proxy_to_original_method
方法,应该不会太难,但是我没有看过.
I believe you could extend the Rspec Mocks::MessageExpectation
class to include the and_proxy_to_original_method
method, shouldn't be too hard, but I haven't looked.
这篇关于期望方法调用并使用RSpec代理到原始方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!