From 7754c35de9eac59b3369f4e712e0ee4909a8473d Mon Sep 17 00:00:00 2001 From: Michal Cichra Date: Wed, 8 Oct 2014 17:36:35 +0200 Subject: [PATCH 1/2] requiring dsl should require the whole library --- lib/rspec_api_documentation/dsl.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/rspec_api_documentation/dsl.rb b/lib/rspec_api_documentation/dsl.rb index 574cabb5..c50afba5 100644 --- a/lib/rspec_api_documentation/dsl.rb +++ b/lib/rspec_api_documentation/dsl.rb @@ -1,3 +1,4 @@ +require "rspec_api_documentation" require "rspec_api_documentation/dsl/resource" require "rspec_api_documentation/dsl/endpoint" require "rspec_api_documentation/dsl/callback" From 9c7350be8aa49dc3d9e4d5d5e420e6f148e82385 Mon Sep 17 00:00:00 2001 From: Michal Cichra Date: Wed, 8 Oct 2014 17:58:14 +0200 Subject: [PATCH 2/2] make dsl work without rspec monkeypatching --- features/example_request.feature | 31 ++++++++++++++++++--- lib/rspec_api_documentation/dsl.rb | 43 ++++++++++++++++++------------ 2 files changed, 53 insertions(+), 21 deletions(-) diff --git a/features/example_request.feature b/features/example_request.feature index ed2a636d..08cff944 100644 --- a/features/example_request.feature +++ b/features/example_request.feature @@ -8,8 +8,10 @@ Feature: Example Request end end """ - And a file named "app_spec.rb" with: - """ + + Scenario: Output should have the correct error line + Given a file named "app_spec.rb" with: + """ require "rspec_api_documentation" require "rspec_api_documentation/dsl" @@ -26,11 +28,32 @@ Feature: Example Request end """ When I run `rspec app_spec.rb --require ./app.rb --format RspecApiDocumentation::ApiFormatter` - - Scenario: Output should have the correct error line Then the output should contain "expected: 201" Then the output should not contain "endpoint.rb" Then the output should contain: """ rspec ./app_spec.rb:10 # Example Request GET / Greeting your favorite gem """ + + Scenario: should work with RSpec monkey patching disabled + When a file named "app_spec.rb" with: + """ + require "rspec_api_documentation/dsl" + + RSpec.configure do |config| + config.disable_monkey_patching! + end + + RspecApiDocumentation.configure do |config| + config.app = App + end + + RSpec.resource "Example Request" do + get "/" do + example_request "Greeting your favorite gem" do + expect(status).to eq(200) + end + end + end + """ + Then I successfully run `rspec app_spec.rb --require ./app.rb` diff --git a/lib/rspec_api_documentation/dsl.rb b/lib/rspec_api_documentation/dsl.rb index c50afba5..07662f14 100644 --- a/lib/rspec_api_documentation/dsl.rb +++ b/lib/rspec_api_documentation/dsl.rb @@ -3,25 +3,34 @@ require "rspec_api_documentation/dsl/endpoint" require "rspec_api_documentation/dsl/callback" -# Custom describe block that sets metadata to enable the rest of RAD -# -# resource "Orders", :meta => :data do -# # ... -# end -# -# Params: -# +args+:: Glob of RSpec's `describe` arguments -# +block+:: Block to pass into describe -# -def self.resource(*args, &block) - options = if args.last.is_a?(Hash) then args.pop else {} end - options[:api_doc_dsl] = :resource - options[:resource_name] = args.first - options[:document] ||= :all - args.push(options) - describe(*args, &block) + +module RspecApiDocumentation + module DSL + + # Custom describe block that sets metadata to enable the rest of RAD + # + # resource "Orders", :meta => :data do + # # ... + # end + # + # Params: + # +args+:: Glob of RSpec's `describe` arguments + # +block+:: Block to pass into describe + # + def resource(*args, &block) + options = if args.last.is_a?(Hash) then args.pop else {} end + options[:api_doc_dsl] = :resource + options[:resource_name] = args.first + options[:document] ||= :all + args.push(options) + describe(*args, &block) + end + end end +RSpec::Core::ExampleGroup.extend(RspecApiDocumentation::DSL) +RSpec::Core::DSL.expose_example_group_alias(:resource) + RSpec.configuration.include RspecApiDocumentation::DSL::Resource, :api_doc_dsl => :resource RSpec.configuration.include RspecApiDocumentation::DSL::Endpoint, :api_doc_dsl => :endpoint RSpec.configuration.include RspecApiDocumentation::DSL::Callback, :api_doc_dsl => :callback