How do you mock a method with a block using RR (double ruby) on Rails 3

1.6k views Asked by At

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?

1

There are 1 answers

3
trcarden On BEST ANSWER

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