虽然我的问题很简单,但我没能在这里找到答案:
如何 stub 方法并返回参数本身(例如在执行数组操作的方法上)?
像这样:
interface.stub!(:get_trace).with(<whatever_here>).and_return(<whatever_here>)
最佳答案
注意:stub 方法已被弃用。请看this answer以现代方式做到这一点。
stub!
可以接受一个 block 。 block 接收参数; block 的返回值是 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/