RSpec:带参数的存根链? [英] RSpec: Stub chains with arguments?

查看:48
本文介绍了RSpec:带参数的存根链?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是想知道是否/如何在 rspec 存根链中传递参数.举个例子,假设我有以下动作:

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

在我的控制器规范中,我希望能够排除这两种方法并返回不同的结果.如果只有 @payments 字段在操作中,我会使用类似

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 }

当然,这将为 @bad_payments 返回相同的值.

But of course, that will return the same value for @bad_payments.

所以 - 简而言之,我如何包含 :updated_at:paid =>true 作为存根条件?

So - in short, how do I include the :updated_at and :paid => true as stub conditions?

推荐答案

你可以使用这个:

Payment.stub_chain(:order, :where).with(:updated_at).with(:paid => true) { return_this }

这篇关于RSpec:带参数的存根链?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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