我正在尝试使用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
但它们都不起作用。
终于得到了它。这个pull请求帮助:https://github.com/btakita/rr/pull/82
mock(RestClient).get(url, {:params => params}).yields(response, request, result) { response }