【问题标题】:Testing if dynamic method call was received using RSpec测试是否使用 RSpec 接收到动态方法调用
【发布时间】:2014-07-22 18:27:57
【问题描述】:

我正在尝试测试是否使用 RSpec 执行了动态方法调用。有点麻烦。

我的代码看起来像:

def self.parse_file
  method_name = "parse_#{get_file_type}"
  send method_name
end

def self.parse_gz
  ....
end

假设get_file_type返回“gz”,我想测试一下parse_gz是从parse_file实例方法中调用的。

最初,我在想类似下面的事情,但我认为我做错了......

Class.should_receive(:parse_gz).with(Class.parse_file)

...这不起作用

非常感谢任何帮助...

【问题讨论】:

  • 只有Class.should_receive(:parse_gz) 有效吗?

标签: ruby rspec


【解决方案1】:

您的版本不起作用,因为您从不使用参数调用parse_gz。试试这个:

Class.should_receive(:parse_file)

Class.should_receive(:send).with('parse_gz')

此外,我建议使用新的allow(thing).to receive(method) 语法而不是旧的thing.should_receive(method) 语法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-05
    • 1970-01-01
    • 2014-02-11
    • 1970-01-01
    • 1970-01-01
    • 2012-10-26
    • 1970-01-01
    相关资源
    最近更新 更多