diff --git a/.gitignore b/.gitignore index 3e3e16a..d6d1164 100644 --- a/.gitignore +++ b/.gitignore @@ -13,4 +13,5 @@ bower_components/* bower_components/swagger-ui/* !bower_components/swagger-ui/dist .ruby-version -.idea \ No newline at end of file +.idea +spec/dummy/swagger/v1_api.json \ No newline at end of file diff --git a/spec/swagger_rails/rspec/formatter_spec.rb b/spec/swagger_rails/rspec/formatter_spec.rb index f1cc06b..66e9944 100644 --- a/spec/swagger_rails/rspec/formatter_spec.rb +++ b/spec/swagger_rails/rspec/formatter_spec.rb @@ -28,19 +28,28 @@ RSpec.describe ::SwaggerRails::RSpec::Formatter do end end - def swaggerize(*groups) + def run_group_and_send_example_group_finished_notification(*groups) groups.each do |group| group.run(reporter) send_notification_for_all_child_groups group end end + def run_group_and_send_stop_notification(*groups) + groups.each do |group| + group.run(reporter) + send_notification_for_all_child_groups group + send_notification :stop, stop_notification + end + end + + let(:swagger_root) { (Rails.root + 'swagger').to_s } + let(:swagger_doc) { 'v1_api.json' } before(:each) do - swagger_root = (Rails.root + 'swagger').to_s SwaggerRails::Configuration.new.tap { |c| c.swagger_root = swagger_root } - allow(RSpec.configuration).to receive(:swagger_docs).and_return({ 'v1_api.json' => { swagger: '2.0', - info: { title: 'API V1', - version: 'v1' } } }) + allow(RSpec.configuration).to receive(:swagger_docs).and_return({ swagger_doc => { swagger: '2.0', + info: { title: 'API V1', + version: 'v1' } } }) allow(RSpec.configuration).to receive(:swagger_root).and_return(swagger_root) @formatter = SwaggerRails::RSpec::Formatter.new(StringIO.new) end @@ -48,13 +57,13 @@ RSpec.describe ::SwaggerRails::RSpec::Formatter do describe '#new' do it 'should initialize the swagger_root' do expect(@formatter.instance_variable_get(:@swagger_root)).to eq((Rails.root + 'swagger').to_s) - expect(@formatter.instance_variable_get(:@swagger_docs)).to eq({"v1_api.json"=>{:swagger=>"2.0", :info=>{:title=>"API V1", :version=>"v1"}}}) + expect(@formatter.instance_variable_get(:@swagger_docs)).to eq({ swagger_doc => { :swagger => "2.0", :info => { :title => "API V1", :version => "v1" } } }) end end describe '#example_group_finished' do before do - swaggerize(group) + run_group_and_send_example_group_finished_notification(group) end it "should print 'Generating Swagger Docs ...'" do @@ -68,12 +77,59 @@ RSpec.describe ::SwaggerRails::RSpec::Formatter do :info => { :title => 'API V1', :version => 'v1' }, :paths => { '/ping' => { :post => { :tags => ['Ping API'], - :summary => 'checks if site is alive', - :description => 'A very long description', - :consumes => ['application/json'], - :produces => ['application/json'], - :parameters => [], - :responses => { '200' => { :description => '(OK) Site up and running' } } } } } } }) + :summary => 'checks if site is alive', + :description => 'A very long description', + :consumes => ['application/json'], + :produces => ['application/json'], + :parameters => [], + :responses => { '200' => { :description => '(OK) Site up and running' } } } } } } }) + end + end + + describe '#stop' do + before(:each) do + run_group_and_send_stop_notification(group) + end + + it 'should write to swagger doc inside swagger root directory' do + file_path = File.join(swagger_root, swagger_doc) + file_context= nil + + File.open(file_path, 'r') do |file| + file_context = file.read + end + + expect(JSON.parse(file_context)).to eq({ 'swagger' => '2.0', + 'info' => { + 'title' => 'API V1', + 'version' => 'v1' + }, + 'paths' => { + '/ping' => { + 'post' => { + 'tags' => [ + 'Ping API' + ], + 'summary' => 'checks if site is alive', + 'description' => 'A very long description', + 'consumes' => [ + 'application/json' + ], + 'produces' => [ + 'application/json' + ], + 'parameters' => [ + + ], + 'responses' => { + '200' => { + 'description' => '(OK) Site up and running' + } + } + } + } + } + }) end end end