1

I'm trying to generate API documentation with rwsag, and I'm confused about a couple of my specs failing.

Here is my request spec:-

require 'swagger_helper' ### this includes 'rails_helper'

RSpec.describe 'api/v1/users', type: :request do
  before(:each) { host! 'localhost:3001' }

  path '/api/v1/users' do

    post('create user') do
      tags 'users'
      consumes 'application/json'
      parameter name: :user, in: :body, schema: {
        type: :object,
        properties: {
          title: { type: :string },
          description: { type: :string },
          date: { type: :datetime },
          budget: { type: :decimal },
          awarded: { type: :boolean }
        },
      required: [ 'title', 'description' ]
    }
      response(200, 'successful') do

        after do |example|
          example.metadata[:response][:content] = {
            'application/json' => {
              example: JSON.parse(response.body, symbolize_names: true)
            }
          }
        end
        run_test!
      end
    end
  end

Here are my two errors which don't make much sense to me:-

1) api/v1/users /api/v1/users post successful returns a 200 response
     Got 0 failures and 2 other errors:

     1.1) Failure/Error: super
          
          NoMethodError:
            undefined method `user' for #<RSpec::ExampleGroups::ApiV1Users::ApiV1Users::Post::Successful:0x0000aaaad7d06600>
          # /usr/local/bundle/gems/rswag-specs-2.4.0/lib/rswag/specs/request_factory.rb:197:in `build_json_payload'
          # /usr/local/bundle/gems/rswag-specs-2.4.0/lib/rswag/specs/request_factory.rb:180:in `add_payload'
          # /usr/local/bundle/gems/rswag-specs-2.4.0/lib/rswag/specs/request_factory.rb:22:in `block in build_request'
          # /usr/local/bundle/gems/rswag-specs-2.4.0/lib/rswag/specs/request_factory.rb:18:in `tap'
          # /usr/local/bundle/gems/rswag-specs-2.4.0/lib/rswag/specs/request_factory.rb:18:in `build_request'
          # /usr/local/bundle/gems/rswag-specs-2.4.0/lib/rswag/specs/example_helpers.rb:10:in `submit_request'
          # /usr/local/bundle/gems/rswag-specs-2.4.0/lib/rswag/specs/example_group_helpers.rb:94:in `block in run_test!'

     1.2) Failure/Error: example: JSON.parse(response.body, symbolize_names: true)
          
          NoMethodError:
            undefined method `body' for nil:NilClass
          # ./spec/requests/api/v1/users_spec.rb:84:in `block (5 levels) in <main>'

And my standard controller action:-

module Api
  module V1
    class UsersController < ApplicationController
# POST /api/v1/users
      def create
        @user = Job.new(user_params)

        if @user.save
          render json: @user, status: :created
        else
          render json: @user.errors, status: :unprocessable_entity
        end
      end

Any idea what is causing this error? I'm also a little confused with the generated rswag spec structure - i.e. no expects anywhere.

4

1 回答 1

1

答案是我错过了以下let(:user)行:-

  response(201, 'successful') do

    after do |example|
      example.metadata[:response][:content] = {
        'application/json' => {
          example: JSON.parse(response.body, symbolize_names: true)
        }
      }
    end
    let(:user) { { title: 'foo', description: 'bar' } }
    run_test!
  end
于 2021-03-15T12:30:03.953 回答