mirror of
https://github.com/ditkrg/rswag.git
synced 2026-01-25 15:22:56 +00:00
More useful error messages in rswag-specs
This commit is contained in:
@@ -5,33 +5,23 @@ module Rswag
|
||||
module Specs
|
||||
|
||||
describe SwaggerFormatter do
|
||||
# Mock infrastructure - output, RSpec.configuration etc.
|
||||
subject { described_class.new(output, config) }
|
||||
|
||||
# Mock out some infrastructure
|
||||
before do
|
||||
allow(config).to receive(:swagger_root).and_return(swagger_root)
|
||||
end
|
||||
let(:config) { double('config') }
|
||||
let(:output) { double('output').as_null_object }
|
||||
let(:swagger_root) { File.expand_path('../tmp', __FILE__) }
|
||||
let(:swagger_docs) do
|
||||
{
|
||||
'v1/swagger.json' => { info: { version: 'v1' } }
|
||||
}
|
||||
end
|
||||
let(:config) { OpenStruct.new(swagger_root: swagger_root, swagger_docs: swagger_docs) }
|
||||
before { allow(RSpec).to receive(:configuration).and_return(config) }
|
||||
|
||||
subject { described_class.new(output) }
|
||||
|
||||
describe '::new(output)' do
|
||||
context 'swagger_root not configured' do
|
||||
let(:swagger_root) { nil }
|
||||
it { expect { subject }.to raise_error ConfigurationError }
|
||||
end
|
||||
|
||||
context 'swagger_docs not configured' do
|
||||
let(:swagger_docs) { nil }
|
||||
it { expect { subject }.to raise_error ConfigurationError }
|
||||
end
|
||||
end
|
||||
let(:swagger_root) { File.expand_path('../tmp/swagger', __FILE__) }
|
||||
|
||||
describe '#example_group_finished(notification)' do
|
||||
# Mock notification parameter
|
||||
before do
|
||||
allow(config).to receive(:get_swagger_doc).and_return(swagger_doc)
|
||||
subject.example_group_finished(notification)
|
||||
end
|
||||
let(:swagger_doc) { {} }
|
||||
let(:notification) { OpenStruct.new(group: OpenStruct.new(metadata: api_metadata)) }
|
||||
let(:api_metadata) do
|
||||
{
|
||||
path: '/blogs',
|
||||
@@ -39,78 +29,35 @@ module Rswag
|
||||
response: { code: '201', description: 'blog created' }
|
||||
}
|
||||
end
|
||||
let(:notification) { OpenStruct.new(group: OpenStruct.new(metadata: api_metadata)) }
|
||||
|
||||
let(:call) { subject.example_group_finished(notification) }
|
||||
|
||||
context 'single swagger_doc' do
|
||||
before { call }
|
||||
|
||||
it 'converts metadata to swagger and merges into the doc' do
|
||||
expect(swagger_docs.values.first).to match(
|
||||
info: { version: 'v1' },
|
||||
paths: {
|
||||
'/blogs' => {
|
||||
post: {
|
||||
summary: 'Creates a blog',
|
||||
responses: {
|
||||
'201' => { description: 'blog created' }
|
||||
}
|
||||
it 'converts to swagger and merges into the corresponding swagger doc' do
|
||||
expect(swagger_doc).to match(
|
||||
paths: {
|
||||
'/blogs' => {
|
||||
post: {
|
||||
summary: 'Creates a blog',
|
||||
responses: {
|
||||
'201' => { description: 'blog created' }
|
||||
}
|
||||
}
|
||||
}
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
context 'multiple swagger_docs' do
|
||||
let(:swagger_docs) do
|
||||
{
|
||||
'v1/swagger.json' => {},
|
||||
'v2/swagger.json' => {}
|
||||
}
|
||||
end
|
||||
|
||||
context "no 'swagger_doc' tag" do
|
||||
before { call }
|
||||
|
||||
it 'merges into the first doc' do
|
||||
expect(swagger_docs.values.first).to have_key(:paths)
|
||||
end
|
||||
end
|
||||
|
||||
context "matching 'swagger_doc' tag" do
|
||||
before do
|
||||
api_metadata[:swagger_doc] = 'v2/swagger.json'
|
||||
call
|
||||
end
|
||||
|
||||
it 'merges into the matched doc' do
|
||||
expect(swagger_docs.values.last).to have_key(:paths)
|
||||
end
|
||||
end
|
||||
|
||||
context "non matching 'swagger_doc' tag" do
|
||||
before { api_metadata[:swagger_doc] = 'foobar' }
|
||||
it { expect { call }.to raise_error ConfigurationError }
|
||||
end
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
describe '#stop' do
|
||||
let(:notification) { double('notification') }
|
||||
let(:swagger_docs) do
|
||||
{
|
||||
'v1/swagger.json' => { info: { version: 'v1' } },
|
||||
'v2/swagger.json' => { info: { version: 'v2' } },
|
||||
}
|
||||
end
|
||||
|
||||
before do
|
||||
FileUtils.rm_r(swagger_root) if File.exists?(swagger_root)
|
||||
allow(config).to receive(:swagger_docs).and_return(
|
||||
'v1/swagger.json' => { info: { version: 'v1' } },
|
||||
'v2/swagger.json' => { info: { version: 'v2' } }
|
||||
)
|
||||
subject.stop(notification)
|
||||
end
|
||||
|
||||
let(:notification) { double('notification') }
|
||||
|
||||
it 'writes the swagger_doc(s) to file' do
|
||||
expect(File).to exist("#{swagger_root}/v1/swagger.json")
|
||||
expect(File).to exist("#{swagger_root}/v2/swagger.json")
|
||||
|
||||
Reference in New Issue
Block a user