mirror of
https://github.com/ditkrg/rswag.git
synced 2026-01-25 15:22:56 +00:00
adds request spec generator shamelessly stollen from rspec-rails-swagger
This commit is contained in:
9
rswag-specs/lib/generators/rspec/swagger/USAGE
Normal file
9
rswag-specs/lib/generators/rspec/swagger/USAGE
Normal file
@@ -0,0 +1,9 @@
|
||||
Description:
|
||||
This creates an RSpec request spec to define Swagger documentation for a
|
||||
controller. It will create a test for each of the controller's methods.
|
||||
|
||||
Example:
|
||||
rails generate rspec:swagger V3::AccountsController
|
||||
|
||||
This will create:
|
||||
spec/requests/v3/accounts_spec.rb
|
||||
@@ -0,0 +1,24 @@
|
||||
require 'rspec/rails/swagger/route_parser'
|
||||
require 'rails/generators'
|
||||
|
||||
module Rspec
|
||||
module Generators
|
||||
class SwaggerGenerator < ::Rails::Generators::NamedBase
|
||||
source_root File.expand_path('../templates', __FILE__)
|
||||
|
||||
def setup
|
||||
@routes = RSpec::Rails::Swagger::RouteParser.new(controller_path).routes
|
||||
end
|
||||
|
||||
def create_spec_file
|
||||
template 'spec.rb', File.join('spec/requests', "#{controller_path}_spec.rb")
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def controller_path
|
||||
file_path.chomp('_controller')
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
30
rswag-specs/lib/generators/rspec/swagger/templates/spec.rb
Normal file
30
rswag-specs/lib/generators/rspec/swagger/templates/spec.rb
Normal file
@@ -0,0 +1,30 @@
|
||||
require 'swagger_helper'
|
||||
|
||||
RSpec.describe '<%= controller_path %>', type: :request do
|
||||
<% @routes.each do | template, path_item | %>
|
||||
path '<%= template %>' do
|
||||
<% unless path_item[:params].empty? -%>
|
||||
# You'll want to customize the parameter types...
|
||||
<% path_item[:params].each do |param| -%>
|
||||
parameter '<%= param %>', in: :body, type: :string
|
||||
<% end -%>
|
||||
<% end -%>
|
||||
<% path_item[:actions].each do | action, details | -%>
|
||||
<%= action %>('<%= details[:summary] %>') do
|
||||
response(200, 'successful') do
|
||||
<% unless path_item[:params].empty? -%>
|
||||
<% path_item[:params].each do |param| -%>
|
||||
let(:<%= param %>) { '123' }
|
||||
<% end -%>
|
||||
<% end -%>
|
||||
|
||||
after do |example|
|
||||
example.metadata[:response][:examples] = { 'application/json' => JSON.parse(response.body, symbolize_names: true) }
|
||||
end
|
||||
run_test!
|
||||
end
|
||||
end
|
||||
<% end -%>
|
||||
end
|
||||
<% end -%>
|
||||
end
|
||||
Reference in New Issue
Block a user