Search code examples
rubyrspecmockingrspec-mocks

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


I want to check that Pandoc.convert is called with to: :docx option like this:

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

I have the following expectation in a spec:

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

The spec fails like this:

 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

But when debugging, options is like this:

[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"
}

I think I'm using the wrong RSpec matcher (or the right one in the wrong way), but I can't get it working.


Solution

  • You just need to expect all of the method arguments:

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

    Or you could use a matcher to be less specific about the first argument, e.g.

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