RSpec: Stub chains with arguments?

坚强是说给别人听的谎言 提交于 2019-11-28 22:36:55

You can use this:

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

With rspec > 3 use this syntax:

expect(Converter).to receive_message_chain("new.update_value").with('test').with(no_args)

instead of stub_chain.

Read more about message chains in the documenation. And here is the argument matchers documentation.

You can use nested stub block. The block can accept arguments, and the return value is used as function return value.

I use tap because stub does not returns the callee. The mock created by double is returned as the result of method order, which where method is stub again.

Payment.stub(:order) { |order|
  double('ordered_payments').tap { |proxy|
    proxy.stub(:where) { |where|
      [order, where]
    }
  }
}

Payment.order(:updated_at).where(:paid => true)
# => returns [:updated_at, {:paid => true}]
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!