使用 Rspec 存根链式方法
我想调用一个只返回一个记录的named_scope,但是named_scope返回一个数组,这不是什么大问题,因为我可以将它与.first链接起来:
Model.named_scope(param).first
这有效,我正在努力解决的是如何存根链接的称呼。 有没有人有关于我如何通过 Rspec 模拟实现这一目标的参考或答案?
I want to call a named_scope that will only return one record, but the named_scope returns an array, that's not a big deal as I can just chain it with .first:
Model.named_scope(param).first
and this works, what I am struggling with is how to stub the chained call. Does anyone have a reference or an answer on how I would go about achieving this with Rspec mocking?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我想出了一些办法。
这允许我调用我的控制器:
它可以工作,但是有更好的解决方案吗?
编辑:
rspec 1.2.6 的发布允许我们使用stub_chain,这意味着它现在可以是:
这是我的首要任务,因为总是检查 api 来了解细节:)
I figured something out.
which allows me to call my controller:
It works, but is there a better solution?
EDIT:
The release of rspec 1.2.6 allows us to use stub_chain meaning it can now be:
This was top of my head, as always check the api for specifics :)
更好的版本
将是:
Better version of
will be:
这个问题很老了,因此在如何完成存根方面几乎没有任何增强。 现在您可以使用stub_chain 方法来存根方法调用链。
例如:
@client = Client.named_scope(param).first
可以使用以下方式进行存根:
Client.stub_chain(:named_scope,:first).and_return(@client = mock(Client))
更多stub_chaining示例:
或者请看一下:
rspecs万岁!!! :)
The question is quite old and hence there are few enhancements in how stubbing can be done. Now you can use stub_chain method to stub a chain of method calls.
For example:
@client = Client.named_scope(param).first
can be stubbed with:
Client.stub_chain(:named_scope,:first).and_return(@client = mock(Client))
More examples of stub_chaining:
or please have a look at:
Long live the rspecs!!! :)
我想这是在控制器规范中?
你自己的建议应该很好用。 另一种可能性是将named_scope调用移到模型中,以完全避免该问题。 这也符合“胖模型,瘦控制器”的建议。
I suppose this is in a controller spec?
Your own suggestion should work fine. Another possibility is to move the named_scope call inside your model, to avoid the issue entirely. This would also be in line with the "fat models, thin controllers" advice.
我认为您已经通过将查询放入可以重用的命名范围中来完成瘦控制器的工作。 这是我在开始使用命名范围之前使用的一些代码。
我可能会编写与您已经编写的代码非常相似的代码,但也许会将其放在一个帮助程序中,以便我可以重用它。 另一件事是使用不同的模拟框架,这可能会给你更多的控制权。 看看 Ryan Bates 在 RSpec 上的 Railscast - 现在有点旧了,但仍然有一些好主意。
I think you've already done the thin controller thing by putting the query into a named scope where it can be reused. Here is some code I used before I started using named scopes.
I would probably write code quite similar to what you have already, but maybe put it in a helper that allows me to reuse it. The other thing is to use a different mocking framework that maybe gives you more control. Have a look at Ryan Bates' railscast on RSpec - it's a bit old now but still some good ideas in there.