Is it possible to use VCR gem in Ruby to record an outgoing message and expect that the correct message is sent?

56 Views Asked by At

This would be the same functionality as Webmock. Except it would take a cassette instead of a string.

Something like expect(Webmock)to receive...

1

There are 1 best solutions below

0
B Seven On BEST ANSWER

There is a config for this: match_requests_on.

Defaults to [:method, :uri]

VCR.configure do |config|
  config.default_cassette_options[:match_requests_on] = [:method, :uri, :body, :headers]
end

There is one more use case. Even if you match method, uri, body and headers, the spec will still pass even if the app does not make the expected requests.

To handle this, I write the url, headers and body as JSON to a file.

Then, in the spec use Webmock to validate it:

      message = JSON.parse(File.read(f), symbolize_names: true)
      expect(WebMock).to have_requested(:post, message[:url])
                     .with(headers: message[:headers], body: message[:body])

Reference: https://nicolasiensen.gitbook.io/vcr/configuration/default_cassette_options#match_requests_on-defaults-to-method-uri-when-it-has-not-been-set

https://nicolasiensen.gitbook.io/vcr/request-matching/body