Reputation: 1733
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.
Upvotes: 2
Views: 1791
Reputation: 1733
The answer is that I was missing the below let(:user)
line:-
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
Upvotes: 2