Rswag: Per-response RSpec metadata

Created on 20 Mar 2017  路  6Comments  路  Source: rswag/rswag

Is it possible to define per-response RSpec metadata, e.g. for using VCR? It's possible by wrapping each response into its own describe block, but that creates an unnecessary level of nesting:

require 'swagger_helper'

RSpec.describe 'My API' do
  path '/v1/{x}' do
    get 'Resolve an X' do
      tags 'X'
      produces 'application/json'
      parameter name: :x, in: :path, schema: {
        type: :string
      }

      describe "Success", vcr: { cassette_name: "rswag_resolve_x_success" } do
        response '200', 'Successful result' do
          schema type: :object,
                 properties: {
                   x: { type: :string }
                 }
          let(:x) { "xxx" }

          run_test!
        end
      end

      describe "Fails", vcr: { cassette_name: "rswag_resolve_x_all_fails" } do
        response '503', 'Service unavailable' do
          let(:x) { "xxx" }
          run_test!
        end
      end
    end
  end
end

Most helpful comment

This here worked for us:

require 'swagger_helper'

RSpec.describe 'My API' do
  path '/v1/{x}' do
    get 'Resolve an X' do
      tags 'X'
      produces 'application/json'
      parameter name: :x, in: :path, schema: { type: :string }

      response '200', 'Successful result', vcr: { cassette_name: 'success' } do
        schema type: :object, properties: { x: { type: :string } }
        let(:x) { 'xxx' }

        run_test!
      end

      response '503', 'Service unavailable', vcr: { cassette_name: 'fails' } do
        let(:x) { 'xxx' }

        run_test!
      end
    end
  end
end

Be sure to configure VCR with configure_rspec_metadata!, see https://relishapp.com/vcr/vcr/v/2-2-5/docs/test-frameworks/usage-with-rspec-metadata for more on this.

All 6 comments

@hannesstruss - please checkout #82 and let me know if this will resolve your issue? Thanks

@domaindrivendev yes, looks great, thanks!

@domaindrivendev @hannesstruss how can I use it with a response? Is there an example you can share?

I am looking for an example like that too

This here worked for us:

require 'swagger_helper'

RSpec.describe 'My API' do
  path '/v1/{x}' do
    get 'Resolve an X' do
      tags 'X'
      produces 'application/json'
      parameter name: :x, in: :path, schema: { type: :string }

      response '200', 'Successful result', vcr: { cassette_name: 'success' } do
        schema type: :object, properties: { x: { type: :string } }
        let(:x) { 'xxx' }

        run_test!
      end

      response '503', 'Service unavailable', vcr: { cassette_name: 'fails' } do
        let(:x) { 'xxx' }

        run_test!
      end
    end
  end
end

Be sure to configure VCR with configure_rspec_metadata!, see https://relishapp.com/vcr/vcr/v/2-2-5/docs/test-frameworks/usage-with-rspec-metadata for more on this.

Is it possible to call VCR like this? I've tried the same way you guys did and it just don't end up calling vcr
and even the run_test!:(

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

martio picture martio  路  5Comments

anjum-ahmed picture anjum-ahmed  路  6Comments

vittoriabitton picture vittoriabitton  路  5Comments

richthedev picture richthedev  路  5Comments

bspellacy picture bspellacy  路  3Comments