【问题标题】:How do you mock a method with a block using RR (double ruby) on Rails 3如何在 Rails 3 上使用 RR(双红宝石)模拟带有块的方法
【发布时间】:2011-11-08 20:14:04
【问题描述】:

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

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

我想模拟批处理调用来模拟我们在那里做的事情。

这是我在 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

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

如何模拟使用 RR 获取块的方法?

【问题讨论】:

    标签: ruby-on-rails-3 testing mocking rr


    【解决方案1】:

    K 所以在查看了打开的票据后,我发现答案是该块的第一个参数是一个 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
    

    希望这可以帮助某人节省一些时间。他们需要将这个小宝石添加到文档中

    【讨论】:

    • 我相信你可以使用 graph_mock.batch.yields
    • 嗯,刚刚看了你的自述补丁:是batch.returns还是batch.yeilds?这也适用于 RR 的 new_instance_of 将双精度绑定到新实例吗?
    • 这个方法对我不起作用。详情在这里:stackoverflow.com/questions/15030235/yield-to-a-block-using-rr
    猜你喜欢
    • 2014-11-20
    • 2017-06-06
    • 1970-01-01
    • 1970-01-01
    • 2023-03-16
    • 1970-01-01
    • 2014-12-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多