我们说我在模型上有一个嵌套的范围调用。
# 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)
答案 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}