ruby - RSpec stub : Return the parameter

标签 ruby parameters rspec return-value stubbing

虽然我的问题很简单,但我没能在这里找到答案:

如何 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/

相关文章:

ruby - 运行 irb 文件时出错

ruby - 破解 rails.vim 以与 Padrino 一起工作

c++ - 函数中的 std::end 和 std::begin (C++)

parameters - 如何在 Inno Setup 中通过 "Run-->Parameters"传递参数?

ruby-on-rails - Capybara 在/test 而不是/spec 中生成测试

ruby-on-rails - RSpec + Jasmine Node

ruby-on-rails - 使用按钮运行 rake 任务

ruby-on-rails - ruby 中的树结构与父子数组格式没有 gem ?

java - 使用 JPS 检查 request.getParameter 是否为 null

ruby-on-rails - 如何 'rake spec' 一个目录,例如楷模?