如何在Rails 3上使用RR(双红宝石)模拟一个带方法的方法

trc*_*den 3 testing mocking rr ruby-on-rails-3

我正在使用Koala gem来发出facebook请求,我有以下代码:

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

我想模拟批量调用来模拟我们在那里做的事情.

这是我在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
Run Code Online (Sandbox Code Playgroud)

问题是block_given?即使在我的源代码中传递了一个块,它也会返回false.

我如何使用RR模拟一个采用块的方法?

trc*_*den 5

因此,在查看打开的票证后,我发现答案是块的第一个参数是RR :: ProcFromBlock,它正是传递给函数的块.以下是对代码的修改以使其工作.

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
Run Code Online (Sandbox Code Playgroud)

希望能帮助别人节省一些时间.他们需要将这个小宝石添加到文档中