我正在尝试使用rr
测试以下代码:
response = RestClient.get(url, {:params => params}){|response, request, result| response }
在香草rspec
中,您可以这样做:
RestClient.should_receive(:get).with(url, {:params => params}).and_yield(response, request, result)
我如何对rr
执行相同操作?
设定:
let(:url) { "http://localhost/" }
let(:params) { {:item_id => 1234, :n => 5} }
let(:response) { Object.new }
let(:request) { Object.new }
let(:result) { Object.new }
我尝试了很多变种:
mock(RestClient).get(url, {:params => params}) { response, request, result }
和
mock(RestClient).get(url, {:params => params}, &proc/lambda{}).return(result)
和
mock(RestClient).get(url, {:params => params}).yields(response, request, result)
和
mock(RestClient).get(url, {:params => params}).returns do |proc_as_block|
response
end
但它们都不起作用。
答案 0 :(得分:3)
终于明白了。此拉取请求有助于:https://github.com/btakita/rr/pull/82
mock(RestClient).get(url, {:params => params}).yields(response, request, result) { response }