使用 RSpec 期望方法调用和代理到原始方法 [英] Expect method call and proxy to original method with RSpec

查看:19
本文介绍了使用 RSpec 期望方法调用和代理到原始方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想发现缺少 BDD 的情况:包括 ActiveRecord::Base.find 方法的参数.所以我的想法是在规范中有这样的东西:

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 期望方法调用和代理到原始方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆