RSpec:交换方法?例如stub另一种方法

时间:2015-07-28 15:31:24

标签: ruby-on-rails ruby rspec

我们说我在模型上有一个嵌套的范围调用。

# model
scope :one, -> { ... }
scope :two, -> { ... }
scope :three, -> { ... }

def self.narrow_set_of_records
  one.two.three
end

为了简化测试,我希望能够two返回all而不是执行其通常的范围,返回one的所有结果。有没有办法用RSpec做到这一点? e.g。

# This doesn't seem to be working
allow(Model).to receive(:two).and_return(:all)

1 个答案:

答案 0 :(得分:-1)

It sounds like you want to actually mock a message chain.

something like this ought to work:

allow(Model).to receive_message_chain(:one, :two) {Model.all}