期望(Class)。接收(:x).with(hash_including(y :: z))不起作用

时间:2016-05-17 15:04:48

标签: ruby rspec mocking rspec-mocks

我想检查是否使用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匹配器(或者错误地使用了正确的RSpec匹配器),但是我无法使用它。

1 个答案:

答案 0 :(得分:2)

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

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