RSpec:带有参数的存根链?
只是想知道参数是否/如何可以在 rspec 存根链中传递。举个例子,假设我有以下操作:
def index
@payments = Payment.order(:updated_at).where(:paid => true)
@bad_payments = Payment.order(:some_other_field).where(:paid => false)
end
在我的控制器规范中,我希望能够删除这两种方法并返回不同的结果。如果操作中只有 @paids
字段,我会使用类似的内容
Payment.stub_chain(:order, :where) { return_this }
,但是当然,这将为 @bad_ payments
返回相同的值。
所以 - 简而言之,我如何包含 :updated_at
和 :paid =>; true 作为存根条件?
Just wondering if/how arguments can be passed in rspec stub chains. To give an example, suppose I have the following action:
def index
@payments = Payment.order(:updated_at).where(:paid => true)
@bad_payments = Payment.order(:some_other_field).where(:paid => false)
end
In my controller spec, I'd like to be able to stub out both methods and return different results. If only the @payments
field were in the action I'd use something like
Payment.stub_chain(:order, :where) { return_this }
But of course, that will return the same value for @bad_payments
.
So - in short, how do I include the :updated_at
and :paid => true
as stub conditions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你可以使用这个:
You can use this:
与 rspec > 3 使用以下语法:
而不是
stub_chain
。文档中了解有关消息链的更多信息。 这里是参数匹配器文档。
With
rspec > 3
use this syntax:instead of
stub_chain
.Read more about message chains in the documenation. And here is the argument matchers documentation.
您可以使用嵌套存根块。该块可以接受参数,并且返回值用作函数返回值。
我使用
tap
因为stub
不返回被调用者。由double
创建的 mock 作为方法order
的结果返回,其中where
方法再次存根。You can use nested stub block. The block can accept arguments, and the return value is used as function return value.
I use
tap
becausestub
does not returns the callee. The mock created bydouble
is returned as the result of methodorder
, whichwhere
method is stub again.