3

I am using the Koala gem to make facebook requests and i have the following code:

  @graph = Koala::Facebook::API.new(oauth_token)
  @graph.batch do |batch_api|
    #... do stuff here
  end

I want to mock out the batch call to simulate the stuff we are doing in there.

Here is what i have in RR.

oauth_token= "Sometoken"
batch_api_mock = nil
graph_mock = mock(Koala::Facebook::API).new(oauth_token).mock!
graph_mock.batch.returns do
  yield batch_api_mock if block_given?
end

The problem is that block_given? is return false even though there is a block being passed in my source.

How do i mock a method that takes a block using RR?

trcarden
  • 881
  • 1
  • 9
  • 17

1 Answers1

5

K so after looking through the open tickets i found that the answer was that the first argument to the block is a RR::ProcFromBlock which is exactly the block that would be passed to the function. Here is the modification to the code to make it work.

oauth_token= "Sometoken"
batch_api_mock = nil
graph_mock = mock(Koala::Facebook::API).new(oauth_token).mock!

#The block is passed in as a proc as the first argument to the returns block.
graph_mock.batch.returns do |proc_as_block|
  proc_as_block.call
end

Hope that helps someone save some time. They need to add this little gem to the documentation

trcarden
  • 881
  • 1
  • 9
  • 17
  • I belive you can use graph_mock.batch.yields – Sebastian Apr 05 '12 at 07:23
  • Hmm, just looked at your readme patch: is it `batch.returns` or `batch.yeilds` ? Also does this work with RR's `new_instance_of` for binding doubles to new instances? – Hedgehog Nov 30 '12 at 21:34
  • This method isn't working for me. Details here: http://stackoverflow.com/questions/15030235/yield-to-a-block-using-rr – Raphael Feb 22 '13 at 18:24