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



我想检查是否使用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))

最新更新