使用rr得到块

时间:2013-02-22 17:50:48

标签: ruby-on-rails ruby rspec rr

我正在尝试使用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

但它们都不起作用。

1 个答案:

答案 0 :(得分:3)

终于明白了。此拉取请求有助于:https://github.com/btakita/rr/pull/82

mock(RestClient).get(url, {:params => params}).yields(response, request, result) { response }