尽管我的问题很简单,但我未能在此处找到答案:

如何 stub 方法并返回参数本身(例如,在执行数组操作的方法上)?

像这样:

 interface.stub!(:get_trace).with(<whatever_here>).and_return(<whatever_here>)

最佳答案

注意:不建议使用stub方法。请参阅this answer以了解执行此操作的现代方法。
stub!可以接受一个块。该块接收参数;该块的返回值是 stub 的返回值:

class Interface
end

describe Interface do
  it "should have a stub that returns its argument" do
    interface = Interface.new
    interface.stub!(:get_trace) do |arg|
      arg
    end
    interface.get_trace(123).should eql 123
  end
end

关于ruby - RSpec stub : Return the parameter,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5938049/

10-12 19:46