README.md in rspec-rails-api-0.6.0 vs README.md in rspec-rails-api-0.6.1

- old
+ new

@@ -24,93 +24,60 @@ ### Rails configuration Configuration should be made manually for now: -**spec/acceptance_helper.rb** +**spec/support/rspec_rails_api.rb**: ```ruby -require 'rails_helper' require 'rspec_rails_api' +# Associate spec/acceptance/* to acceptance tests +RSpec::Rails::DIRECTORY_MAPPINGS[:acceptance] = %w[spec acceptance] + RSpec.configure do |config| config.include RSpec::Rails::Api::DSL::Example -end + config.include RSpec::Rails::RequestExampleGroup, type: :acceptance -renderer = RSpec::Rails::Api::OpenApiRenderer.new -# Options here should be customized -renderer.api_title = 'YourProject API' -renderer.api_version = '1' -renderer.api_description = 'Manage data on YourProject' -# Options below are optional -renderer.api_servers = [{ url: 'https://example.com' }] -renderer.api_tos = 'http://example.com/tos.html' -renderer.api_contact = { name: 'Admin', email: 'admin@example.com', url: 'http://example.com/contact' } -renderer.api_license = { name: 'Apache', url: 'https://opensource.org/licenses/Apache-2.0' } + # Define the renderer if you want to generate the OpenApi documentation + renderer = RSpec::Rails::Api::OpenApiRenderer.new + # Options here should be customized + renderer.api_title = 'YourProject API' + renderer.api_version = '1' + renderer.api_description = 'Manage data on YourProject' + # Options below are optional + renderer.api_servers = [{ url: 'https://example.com' }] + renderer.api_tos = 'http://example.com/tos.html' + renderer.api_contact = { name: 'Admin', email: 'admin@example.com', url: 'http://example.com/contact' } + renderer.api_license = { name: 'Apache', url: 'https://opensource.org/licenses/Apache-2.0' } + # ... Check the "Configuration" section for all the options + + config.after(:context, type: :acceptance) do |context| + renderer.merge_context context.class.metadata[:rra].to_h + end -RSpec.configuration.after(:context, type: :acceptance) do |context| - renderer.merge_context context.class.metadata[:rra].to_h + config.after(:suite) do + # Default path is 'tmp/rspec_rails_api_output.json/yaml' + renderer.write_files Rails.root.join('public', 'swagger_doc'), only: [:json] + end end - -RSpec.configuration.after(:suite) do - # Default path is 'tmp/rspec_rails_api_output.json/yaml' - renderer.write_files Rails.root.join('public', 'swagger_doc'), only: [:json] -end ``` -**spec/rails_helper.rb** +**spec/rails_helper.rb**: ```ruby -# ... - -RSpec::Rails::DIRECTORY_MAPPINGS[:acceptance] = %w[spec acceptance] - -RSpec.configure do |config| - # ... - config.include RSpec::Rails::RequestExampleGroup, type: :acceptance -end +#... +require 'support/rspec_rails_api' +#... ``` ## Configuration **TODO: This section is incomplete and the gem has no generator yet.** -Here is an example configuration: ```rb -# spec/rails_helper.rb -require 'spec/support/rspec_rails_api' -``` - -```rb -# spec/support/acceptance_entities.rb - -# This file contains common object definitions -{ - error: { - error: { type: :string, description: "Error message" } - }, - form_error: { - title: { type: :array, required: false, description: "Title errors", of: :string } - }, -}.each do |name, attributes| - RSpec::Rails::Api::Metadata.add_entity name, attributes -end -``` - -```rb -# spec/support/rspec_rails_api.rb -require 'rspec_rails_api' -require 'support/acceptance_entities' - -# Include DSL in RSpec -RSpec.configure do |config| - config.include RSpec::Rails::Api::DSL::Example -end - -# Initialize and configure the renderer -renderer = RSpec::Rails::Api::OpenApiRenderer.new # Server URL for quick reference server_url = 'https://example.com' # Options here should be present for a valid OpenAPI file renderer.api_title = 'MyProject API' @@ -227,14 +194,10 @@ ``` This solution comes from [this article](https://makandracards.com/makandra/37161-rspec-devise-how-to-sign-in-users-in-request-specs) by Arne Hartherz (MIT license). -## Usage - -Write some spec files and run RSpec as you would usually do. - ## Writing specs There is a [commented example](dummy/spec/acceptance/posts_spec.rb) available in `dummy/spec/acceptance`. @@ -332,25 +295,27 @@ ``` Organization of the global entities declaration is up to you. +For small projects, we usually put them all in one file: + ```rb -# spec/acceptance/api/users_acceptance_spec.rb -require 'rails_helper' +# spec/support/acceptance_entities.rb -RSpec.describe 'Users', type: :acceptance do - resource 'Users', 'Manage users' +require 'rspec/rails/api/metadata' - on_get '/api/users/', 'Users list' do - # "user" will use the global user entity - for_code 200, 'Success response', expect_many: :user do |url| - test_response_of url - end - end - - #... +# This file contains common object definitions +{ + error: { + error: { type: :string, description: "Error message" } + }, + form_error: { + title: { type: :array, required: false, description: "Title errors", of: :string } + }, +}.each do |name, attributes| + RSpec::Rails::Api::Metadata.add_entity name, attributes end ``` ### DSL @@ -743,9 +708,14 @@ MRs to improve this are welcome. ### Files There is no support for file fields yet. + +### Security + +`Security` mechanisms are only declared for the OpenApi documentation as a reference, and is +not checked nor enforced in the tests. ## Development After checking out the repo, run `bin/setup` to install dependencies. Then, run `bundle exec rspec` to run the tests. You can also run `bin/console`