Rswag: How to mock request and response with webmock or VCR

Created on 21 Mar 2019  路  5Comments  路  Source: rswag/rswag

Hello,

I'm documenting the API of my project, but since one request has others behind rather than just the authentication, I've decided to mock the request at all. I already use webmock and VCR in the project, so I would like to keep using it for the integration test. There are any examples for doing it?

Thanks in advance.

Most helpful comment

I made it work using the alternative syntax, apparently, this is the only possible way.

Using vcr (6.0.0) and rswag (2.3.1)

response '200', 'OK' do
  it 'returns a valid 201 response' do |example|
    VCR.use_cassette('success') do
      submit_request(example.metadata)
      assert_response_matches_metadata(example.metadata)
    end
  end
end

All 5 comments

@vittoriabitton Not sure if you saw this: https://github.com/domaindrivendev/rswag/issues/50#issuecomment-481777011

It worked for me.

@Petercopter i've tried using your example...it doesn't call VCR, and I discovered that it does not even call the run_test!...don't know where the problem might be.

OBS: i've tried using vcr in the response as metadata...same result.

```ruby require 'swagger_helper'

RSpec.describe 'Orchestration API', type: :request, swagger_doc: 'v1/swagger.json' do

let(:operation_attributes) do
{
workflow_key: 'process',
id: '11',
decision_variables: { score: { value: 400 } }
}
end

path '/api/v1/orchestration/operations/start' do
post 'Start a new process' do
tags 'Operations'
consumes 'application/json'
parameter name: :operation_attributes, in: :body, type: :object
VCR.use_cassette 'api/v1/orchestration/operations/start/ok' do
response '200', 'OK' do
schema type: 'orchestration/operation',
properties: {
data: {
type: :object,
items: {
'reference-id': { type: :string },
'process-instance-id': { type: :string },
'workflow-id': { type: :string },
'workflow-name': { type: :string },
'discarded': { type: :boolean },
'status': { type: :string },
'delete-reason': { type: :string },
'start-at': { type: :string },
'end-at': { type: :string },
'parent-operation-id': { type: :string }
},
relationships: {
activities: {
data: []
}
}
}
}
run_test!
end
end
end
end
end```

I made it work using the alternative syntax, apparently, this is the only possible way.

Using vcr (6.0.0) and rswag (2.3.1)

response '200', 'OK' do
  it 'returns a valid 201 response' do |example|
    VCR.use_cassette('success') do
      submit_request(example.metadata)
      assert_response_matches_metadata(example.metadata)
    end
  end
end

@prem-prakash your suggestion works, thanks.

Also we can patch run_test! method

require 'rswag/specs/example_group_helpers'

module Rswag::Specs::ExampleGroupHelpers
  alias_method :run_test_orig!, :run_test!

  def run_test!(vcr: nil, &block)
    if vcr
      around do |example|
        VCR.use_cassette(vcr) do
          example.run
        end
      end
    end

    run_test_orig!(&block)
  end
end

and use it as

run_test! vcr: 'external_service_success'
Was this page helpful?
0 / 5 - 0 ratings