expect(Class).to receive(:x).with(hash_including(y: :z)) 不起作用

expect(Class).to receive(:x).with(hash_including(y: :z)) doesn't work

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

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 匹配器(或者以错误的方式使用了正确的匹配器),但我无法让它工作。

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

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))