【问题标题】:expect(Class).to receive(:x).with(hash_including(y: :z)) doesn't workexpect(Class).to receive(:x).with(hash_including(y: :z)) 不起作用
【发布时间】:2016-05-17 15:04:47
【问题描述】:

我想检查 Pandoc.convert 是否使用 to: :docx 选项调用,如下所示:

options = {to: :docx}
PandocRuby.convert("some string", options)

我对规范有以下期望:

expect(PandocRuby).to receive(:convert).with(hash_including(to: :docx))

规范失败如下:

 Failure/Error: expect(PandocRuby).to receive(:convert).with(hash_including(to: :docx))

   (PandocRuby (class)).convert(hash_including(:to=>:docx))
       expected: 1 time with arguments: (hash_including(:to=>:docx))
       received: 0 times

但是调试的时候options是这样的:

[2] pry(#<ReportDocumentsController>)> options
=> {
    :to => :docx,
    :reference_docx => "/Users/josh/Documents/Work/Access4All/Projects/a4aa2/src/public/uploads/report_template/reference_docx/1/reference.docx"
}

我认为我使用了错误的 RSpec 匹配器(或以错误的方式使用正确的匹配器),但我无法使其正常工作。

【问题讨论】:

    标签: ruby rspec mocking rspec-mocks


    【解决方案1】:

    你只需要期待所有的方法参数:

    expect(PandocRuby).to receive(:convert).with("some string", hash_including(to: :docx))
    

    或者您可以使用匹配器对第一个参数不那么具体,例如

    expect(PandocRuby).to receive(:convert).with(an_instance_of(String), hash_including(to: :docx))
    

    【讨论】:

      猜你喜欢
      • 2012-10-14
      • 2012-11-21
      • 1970-01-01
      • 2013-02-26
      • 1970-01-01
      • 1970-01-01
      • 2022-01-12
      • 2012-01-07
      • 1970-01-01
      相关资源
      最近更新 更多