【问题标题】:rspec stubbing passing the same argumentsrspec 存根传递相同的参数
【发布时间】:2016-09-18 17:37:45
【问题描述】:

示例代码:

class Foo
  def initialize(abc)
    @abc = abc
    @bind = bar
  end
  def bar
    SomeClass.new(@abc)
  end
end

现在我想使用 rspec 和自定义存根来存根 bar:

allow('Foo').to receive(:bar).and_return(FakeBar.new)

问题是FakeBar.new 必须使用:bar 接收的相同参数进行初始化。在我们存根并在存根类中重用它们时,是否可以获得传递给:bar 的参数副本?

【问题讨论】:

    标签: ruby rspec


    【解决方案1】:

    不知道你为什么要做你正在做的事情(可能有一个更简单的方法),但是为了它的价值:

    allow("Foo").to receive(:bar) { |arg1, arg2| FakeBar.new(arg1, arg2) }
    

    RSpec docs, block stub implementation

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-04-15
      • 2011-12-21
      • 2019-03-30
      • 2011-06-02
      • 1970-01-01
      • 2016-03-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多