Search code examples
ruby-on-railsrubyrspecrr

yield to a block using rr


I'm trying to test the following code using rr:

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

In vanilla rspec, you would do something like this:

RestClient.should_receive(:get).with(url, {:params => params}).and_yield(response, request, result)

How would I do the same with rr?

Setup:

let(:url) { "http://localhost/" }
let(:params) { {:item_id => 1234, :n => 5} }
let(:response) { Object.new }
let(:request) { Object.new }
let(:result) { Object.new }

I've tried a bunch of variations on:

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

and

mock(RestClient).get(url, {:params => params}, &proc/lambda{}).return(result)

and

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

and

mock(RestClient).get(url, {:params => params}).returns do |proc_as_block|
  response
end

but none of them work.


Solution

  • Finally got it. This pull request helped: https://github.com/btakita/rr/pull/82

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