From a772398d350e33c9575063eafe2bd277da11c92d Mon Sep 17 00:00:00 2001 From: Eric Oestrich Date: Wed, 21 May 2014 14:59:36 -0400 Subject: [PATCH 01/10] Remove deprecation warnings from spec files Deprecations inside of RAD still exist --- Gemfile.lock | 20 +++++++------ rspec_api_documentation.gemspec | 3 +- spec/api_documentation_spec.rb | 4 +-- spec/configuration_spec.rb | 4 +-- spec/dsl_spec.rb | 38 ++++++++++++------------- spec/example_spec.rb | 28 +++++++++--------- spec/rack_test_client_spec.rb | 16 +++++------ spec/spec_helper.rb | 1 + spec/writers/html_writer_spec.rb | 2 +- spec/writers/json_iodocs_writer_spec.rb | 2 +- spec/writers/json_writer_spec.rb | 2 +- spec/writers/markdown_writer_spec.rb | 2 +- spec/writers/textile_writer_spec.rb | 2 +- 13 files changed, 65 insertions(+), 59 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index ff1e82fd..da9b6262 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -6,7 +6,7 @@ PATH i18n (>= 0.1.0) json (>= 1.4.6) mustache (>= 0.99.4) - rspec (~> 2.14.0) + rspec (~> 2.99.0.rc1) GEM remote: http://rubygems.org/ @@ -80,14 +80,17 @@ GEM rack-test (0.6.2) rack (>= 1.0) rake (10.1.0) - rspec (2.14.1) - rspec-core (~> 2.14.0) - rspec-expectations (~> 2.14.0) - rspec-mocks (~> 2.14.0) - rspec-core (2.14.8) - rspec-expectations (2.14.5) + rspec (2.99.0.rc1) + rspec-core (= 2.99.0.rc1) + rspec-expectations (= 2.99.0.rc1) + rspec-mocks (= 2.99.0.rc1) + rspec-core (2.99.0.rc1) + rspec-expectations (2.99.0.rc1) diff-lcs (>= 1.1.3, < 2.0) - rspec-mocks (2.14.6) + rspec-its (1.0.1) + rspec-core (>= 2.99.0.beta1) + rspec-expectations (>= 2.99.0.beta1) + rspec-mocks (2.99.0.rc1) safe_yaml (0.9.7) sinatra (1.4.4) rack (~> 1.4) @@ -120,6 +123,7 @@ DEPENDENCIES rack-oauth2 (>= 0.14.4) rack-test (>= 0.6.2) rake + rspec-its rspec_api_documentation! sinatra webmock (>= 1.7.0) diff --git a/rspec_api_documentation.gemspec b/rspec_api_documentation.gemspec index 2187c4e2..6ef735e5 100644 --- a/rspec_api_documentation.gemspec +++ b/rspec_api_documentation.gemspec @@ -14,7 +14,7 @@ Gem::Specification.new do |s| s.required_rubygems_version = ">= 1.3.6" - s.add_runtime_dependency "rspec", "~> 2.14.0" + s.add_runtime_dependency "rspec", "~> 2.99.0.rc1" s.add_runtime_dependency "activesupport", ">= 3.0.0" s.add_runtime_dependency "i18n", ">= 0.1.0" s.add_runtime_dependency "mustache", ">= 0.99.4" @@ -28,6 +28,7 @@ Gem::Specification.new do |s| s.add_development_dependency "rack-test", ">= 0.6.2" s.add_development_dependency "rack-oauth2", ">= 0.14.4" s.add_development_dependency "webmock", ">= 1.7.0" + s.add_development_dependency "rspec-its" s.files = Dir.glob("lib/**/*") + Dir.glob("templates/**/*") s.require_path = 'lib' diff --git a/spec/api_documentation_spec.rb b/spec/api_documentation_spec.rb index 5119bc28..c085b687 100644 --- a/spec/api_documentation_spec.rb +++ b/spec/api_documentation_spec.rb @@ -18,8 +18,8 @@ FileUtils.stub(:cp_r) subject.clear_docs - File.directory?(configuration.docs_dir).should be_true - File.exists?(test_file).should be_false + File.directory?(configuration.docs_dir).should be_truthy + File.exists?(test_file).should be_falsey end end diff --git a/spec/configuration_spec.rb b/spec/configuration_spec.rb index f72f97c8..92ec75fc 100644 --- a/spec/configuration_spec.rb +++ b/spec/configuration_spec.rb @@ -50,7 +50,7 @@ its(:app) { should be_nil } its(:curl_headers_to_filter) { should be_nil } its(:curl_host) { should be_nil } - its(:keep_source_order) { should be_false } + its(:keep_source_order) { should be_falsey } its(:api_name) { should == "API Documentation" } its(:client_method) { should == :client } its(:io_docs_protocol) { should == "http" } @@ -113,7 +113,7 @@ subject.define_group(:sub) do |config| end - subject.groups.should have(1).group + expect(subject.groups.count).to eq(1) end end end diff --git a/spec/dsl_spec.rb b/spec/dsl_spec.rb index 03c9e49d..97425886 100644 --- a/spec/dsl_spec.rb +++ b/spec/dsl_spec.rb @@ -3,17 +3,17 @@ require 'net/http' describe "Non-api documentation specs" do - it "should not be polluted by the rspec api dsl" do + it "should not be polluted by the rspec api dsl" do |example| example.example_group.should_not include(RspecApiDocumentation::DSL) end end resource "Order" do describe "example metadata" do - subject { example.metadata } + subject { |example| example.metadata } its([:resource_name]) { should eq("Order") } - its([:document]) { should be_true } + its([:document]) { should be_truthy } end describe "example context" do @@ -28,10 +28,10 @@ [:post, :get, :put, :delete, :head, :patch].each do |http_method| send(http_method, "/path") do - specify { example.example_group.description.should eq("#{http_method.to_s.upcase} /path") } + specify { |example| example.example_group.description.should eq("#{http_method.to_s.upcase} /path") } describe "example metadata" do - subject { example.metadata } + subject { |example| example.metadata } its([:method]) { should eq(http_method) } its([:route]) { should eq("/path") } @@ -62,7 +62,7 @@ let(:size) { "medium" } describe "example metadata" do - subject { example.metadata } + subject { |example| example.metadata } it "should include the documentated parameters" do subject[:parameters].should eq( @@ -130,7 +130,7 @@ end describe "no_doc" do - it "should not add requests" do + it "should not add requests" do |example| example.metadata[:requests] = ["first request"] no_doc do @@ -179,14 +179,14 @@ describe "nested parameters" do parameter :per_page, "Number of results on a page" - it "should only have 1 parameter" do + it "should only have 1 parameter" do |example| example.metadata[:parameters].length.should == 1 end context "another parameter" do parameter :page, "Current page" - it 'should have 2 parameters' do + it 'should have 2 parameters' do |example| example.metadata[:parameters].length.should == 2 end end @@ -228,12 +228,12 @@ it "should mock requests to the callback url to be handled by the destination" do called = false - destination.stub(:call).and_return do + destination.stub(:call) do called = true [200, {}, []] end do_callback - called.should be_true + called.should be_truthy end end @@ -255,7 +255,7 @@ get "/users/:id/orders" do example "Page should be in the query string" do - client.should_receive(method).with do |path, data, headers| + client.should_receive(method) do |path, data, headers| path.should =~ /^\/users\/1\/orders\?/ path.split("?")[1].split("&").sort.should == "page=2&message=Thank+you".split("&").sort data.should be_nil @@ -290,7 +290,7 @@ context "#explanation" do post "/orders" do - example "Creating an order" do + example "Creating an order" do |example| explanation "By creating an order..." example.metadata[:explanation].should == "By creating an order..." end @@ -305,7 +305,7 @@ let(:id_eq) { [1, 2] } example "parsed properly" do - client.should_receive(:get).with do |path, data, headers| + client.should_receive(:get) do |path, data, headers| Rack::Utils.parse_nested_query(path.gsub('/orders?', '')).should eq({"id_eq"=>['1', '2']}) end do_request @@ -318,7 +318,7 @@ let(:within_id) { {"first" => 1, "last" => 10, "exclude" => [3,5,7]} } example "parsed properly" do - client.should_receive(:get).with do |path, data, headers| + client.should_receive(:get) do |path, data, headers| Rack::Utils.parse_nested_query(path.gsub('/orders?', '')).should eq({ "search" => { "within_id" => {"first" => '1', "last" => '10', "exclude" => ['3','5','7']}} }) @@ -387,7 +387,7 @@ put "/orders" do header "Accept", "application/json" - it "should be sent with the request" do + it "should be sent with the request" do |example| example.metadata[:headers].should == { "Accept" => "application/json" } end @@ -418,7 +418,7 @@ let(:accept) { "application/json" } - it "should be sent with the request" do + it "should be sent with the request" do |example| example.metadata[:headers].should == { "Accept" => :accept } end @@ -448,13 +448,13 @@ resource "top level parameters" do parameter :page, "Current page" - it 'should have 1 parameter' do + it 'should have 1 parameter' do |example| example.metadata[:parameters].length.should == 1 end end resource "passing in document to resource", :document => :not_all do - it "should have the correct tag" do + it "should have the correct tag" do |example| example.metadata[:document].should == :not_all end end diff --git a/spec/example_spec.rb b/spec/example_spec.rb index 932b1331..fc2d97eb 100644 --- a/spec/example_spec.rb +++ b/spec/example_spec.rb @@ -48,25 +48,25 @@ context "when the example's metadata defines a resource name and its document setting is truthy" do let(:metadata) {{ :resource_name => "foo", :document => true }} - it { should be_true } + it { should be_truthy } end context "when the example's metadata does not define a resource name" do let(:metadata) {{ :document => true }} - it { should be_false } + it { should be_falsey } end context "when the example's metadata document setting is falsy" do let(:metadata) {{ :resource_name => "foo", :document => false }} - it { should be_false } + it { should be_falsey } end context "when the example is pending" do let(:rspec_example) { rspec_example_group.pending(description, metadata) {} } - it { should be_false } + it { should be_falsey } end context "configuration sets a filter" do @@ -78,19 +78,19 @@ context "when the example does match the filter" do let(:metadata) { { :resource_name => "foo", :document => :public } } - it { should be_true } + it { should be_truthy } end context "when the example does not match the filter" do let(:metadata) { { :resource_name => "foo", :document => :private } } - it { should be_false } + it { should be_falsey } end context "when the example is excluded" do let(:metadata) { { :resource_name => "foo", :document => [:public, :excluded] } } - it { should be_false } + it { should be_falsey } end end @@ -102,13 +102,13 @@ context "when example doesn't match exclusion" do let(:metadata) { { :resource_name => "foo", :document => :public } } - it { should be_true } + it { should be_truthy } end context "when example matches exclusion" do let(:metadata) { { :resource_name => "foo", :document => [:public, :excluded] } } - it { should be_false } + it { should be_falsey } end end end @@ -119,13 +119,13 @@ context "when the example's metadata public setting is truthy" do let(:metadata) {{ :public => true }} - it { should be_true } + it { should be_truthy } end context "when the example's metadata public setting is falsy" do let(:metadata) {{ :public => false }} - it { should be_false } + it { should be_falsey } end end @@ -135,17 +135,17 @@ context "when parameters are defined" do before { example.stub(:parameters).and_return([double]) } - it { should be_true } + it { should be_truthy } end context "when parameters are empty" do before { example.stub(:parameters).and_return([]) } - it { should be_false } + it { should be_falsey } end context "when parameters are not defined" do - it { should be_false } + it { should be_falsey } end end diff --git a/spec/rack_test_client_spec.rb b/spec/rack_test_client_spec.rb index 7e7309e2..d3ce5b28 100644 --- a/spec/rack_test_client_spec.rb +++ b/spec/rack_test_client_spec.rb @@ -30,7 +30,7 @@ class StubApp < Sinatra::Base end describe RspecApiDocumentation::RackTestClient do - let(:context) { double(:app => StubApp, :example => example) } + let(:context) { |example| double(:app => StubApp, :example => example) } let(:test_client) { RspecApiDocumentation::RackTestClient.new(context, {}) } subject { test_client } @@ -38,8 +38,8 @@ class StubApp < Sinatra::Base it { should be_a(RspecApiDocumentation::RackTestClient) } its(:context) { should equal(context) } - its(:example) { should equal(example) } - its(:metadata) { should equal(example.metadata) } + its(:example) { |example| should equal(example) } + its(:metadata) { |example| should equal(example.metadata) } describe "xml data", :document => true do before do @@ -50,7 +50,7 @@ class StubApp < Sinatra::Base test_client.response_headers["Content-Type"].should =~ /application\/xml/ end - it "should log the request" do + it "should log the request" do |example| example.metadata[:requests].first[:response_body].should be_present end end @@ -86,7 +86,7 @@ class StubApp < Sinatra::Base end context "when examples should be documented", :document => true do - it "should still argument the metadata" do + it "should still argument the metadata" do |example| metadata = example.metadata[:requests].first metadata[:request_query_parameters].should == {'query' => nil, 'other' => 'exists'} end @@ -102,7 +102,7 @@ class StubApp < Sinatra::Base let(:headers) { { "Content-Type" => "application/json;charset=utf-8", "X-Custom-Header" => "custom header value" } } context "when examples should be documented", :document => true do - it "should augment the metadata with information about the request" do + it "should augment the metadata with information about the request" do |example| metadata = example.metadata[:requests].first metadata[:request_method].should eq("POST") metadata[:request_path].should eq("/greet?query=test+query") @@ -122,7 +122,7 @@ class StubApp < Sinatra::Base context "when post data is not json" do let(:post_data) { { :target => "nurse", :email => "email@example.com" } } - it "should not nil out request_body" do + it "should not nil out request_body" do |example| body = example.metadata[:requests].first[:request_body] body.should =~ /target=nurse/ body.should =~ /email=email%40example\.com/ @@ -132,7 +132,7 @@ class StubApp < Sinatra::Base context "when post data is nil" do let(:post_data) { } - it "should nil out request_body" do + it "should nil out request_body" do |example| example.metadata[:requests].first[:request_body].should be_nil end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 630d6f12..0e1b027e 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,5 +1,6 @@ require 'rspec_api_documentation' require 'fakefs/spec_helpers' +require 'rspec/its' RSpec.configure do |config| config.include FakeFS::SpecHelpers diff --git a/spec/writers/html_writer_spec.rb b/spec/writers/html_writer_spec.rb index a43c8ba2..de3401d1 100644 --- a/spec/writers/html_writer_spec.rb +++ b/spec/writers/html_writer_spec.rb @@ -28,7 +28,7 @@ it "should write the index" do writer.write index_file = File.join(configuration.docs_dir, "index.html") - File.exists?(index_file).should be_true + File.exists?(index_file).should be_truthy end end end diff --git a/spec/writers/json_iodocs_writer_spec.rb b/spec/writers/json_iodocs_writer_spec.rb index efa9f75a..44b61c5b 100644 --- a/spec/writers/json_iodocs_writer_spec.rb +++ b/spec/writers/json_iodocs_writer_spec.rb @@ -25,7 +25,7 @@ it "should write the index" do writer.write index_file = File.join(configuration.docs_dir, "apiconfig.json") - File.exists?(index_file).should be_true + File.exists?(index_file).should be_truthy end end end diff --git a/spec/writers/json_writer_spec.rb b/spec/writers/json_writer_spec.rb index d32d13f3..e7e09d79 100644 --- a/spec/writers/json_writer_spec.rb +++ b/spec/writers/json_writer_spec.rb @@ -24,7 +24,7 @@ it "should write the index" do writer.write index_file = File.join(configuration.docs_dir, "index.json") - File.exists?(index_file).should be_true + File.exists?(index_file).should be_truthy end end end diff --git a/spec/writers/markdown_writer_spec.rb b/spec/writers/markdown_writer_spec.rb index 24f295c1..9f40aa4b 100644 --- a/spec/writers/markdown_writer_spec.rb +++ b/spec/writers/markdown_writer_spec.rb @@ -28,7 +28,7 @@ it "should write the index" do writer.write index_file = File.join(configuration.docs_dir, "index.markdown") - File.exists?(index_file).should be_true + File.exists?(index_file).should be_truthy end end diff --git a/spec/writers/textile_writer_spec.rb b/spec/writers/textile_writer_spec.rb index 9d5e4e94..27d4474c 100644 --- a/spec/writers/textile_writer_spec.rb +++ b/spec/writers/textile_writer_spec.rb @@ -28,7 +28,7 @@ it "should write the index" do writer.write index_file = File.join(configuration.docs_dir, "index.textile") - File.exists?(index_file).should be_true + File.exists?(index_file).should be_truthy end end From b47795f2a5a514e1bca031082de2d84ccbc18b05 Mon Sep 17 00:00:00 2001 From: Eric Oestrich Date: Wed, 21 May 2014 16:32:37 -0400 Subject: [PATCH 02/10] Access the current example through new RSpec.current_example --- lib/rspec_api_documentation/dsl/endpoint.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/rspec_api_documentation/dsl/endpoint.rb b/lib/rspec_api_documentation/dsl/endpoint.rb index 61b595f0..f96636cd 100644 --- a/lib/rspec_api_documentation/dsl/endpoint.rb +++ b/lib/rspec_api_documentation/dsl/endpoint.rb @@ -108,6 +108,10 @@ def explanation(text) private + def example + RSpec.current_example + end + def rspec_api_documentation_client send(RspecApiDocumentation.configuration.client_method) end From 9803e1fa13816045bbeefa8fd2bf2c356d746ff1 Mon Sep 17 00:00:00 2001 From: Eric Oestrich Date: Wed, 21 May 2014 17:32:39 -0400 Subject: [PATCH 03/10] Remove deprecation warnings --- Gemfile.lock | 22 ++-- lib/rspec_api_documentation/api_formatter.rb | 24 ++--- lib/rspec_api_documentation/dsl/endpoint.rb | 7 +- rspec_api_documentation.gemspec | 2 +- spec/api_documentation_spec.rb | 33 +++--- spec/api_formatter_spec.rb | 36 ++++--- spec/configuration_spec.rb | 24 ++--- spec/curl_spec.rb | 12 +-- spec/dsl_spec.rb | 106 +++++++++---------- spec/example_spec.rb | 26 ++--- spec/index_spec.rb | 2 +- spec/rack_test_client_spec.rb | 44 ++++---- spec/rspec_api_documentation_spec.rb | 8 +- spec/spec_helper.rb | 1 + spec/views/html_example_spec.rb | 4 +- spec/writers/combined_text_example_spec.rb | 14 +-- spec/writers/html_writer_spec.rb | 6 +- spec/writers/index_helper_spec.rb | 8 +- spec/writers/json_iodocs_writer_spec.rb | 8 +- spec/writers/json_writer_spec.rb | 6 +- spec/writers/markdown_writer_spec.rb | 7 +- spec/writers/textile_writer_spec.rb | 7 +- 22 files changed, 202 insertions(+), 205 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index da9b6262..2af90ed8 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -6,7 +6,7 @@ PATH i18n (>= 0.1.0) json (>= 1.4.6) mustache (>= 0.99.4) - rspec (~> 2.99.0.rc1) + rspec (~> 3.0.0.rc1) GEM remote: http://rubygems.org/ @@ -80,17 +80,21 @@ GEM rack-test (0.6.2) rack (>= 1.0) rake (10.1.0) - rspec (2.99.0.rc1) - rspec-core (= 2.99.0.rc1) - rspec-expectations (= 2.99.0.rc1) - rspec-mocks (= 2.99.0.rc1) - rspec-core (2.99.0.rc1) - rspec-expectations (2.99.0.rc1) - diff-lcs (>= 1.1.3, < 2.0) + rspec (3.0.0.rc1) + rspec-core (= 3.0.0.rc1) + rspec-expectations (= 3.0.0.rc1) + rspec-mocks (= 3.0.0.rc1) + rspec-core (3.0.0.rc1) + rspec-support (= 3.0.0.rc1) + rspec-expectations (3.0.0.rc1) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (= 3.0.0.rc1) rspec-its (1.0.1) rspec-core (>= 2.99.0.beta1) rspec-expectations (>= 2.99.0.beta1) - rspec-mocks (2.99.0.rc1) + rspec-mocks (3.0.0.rc1) + rspec-support (= 3.0.0.rc1) + rspec-support (3.0.0.rc1) safe_yaml (0.9.7) sinatra (1.4.4) rack (~> 1.4) diff --git a/lib/rspec_api_documentation/api_formatter.rb b/lib/rspec_api_documentation/api_formatter.rb index e37e91ea..d931c09d 100644 --- a/lib/rspec_api_documentation/api_formatter.rb +++ b/lib/rspec_api_documentation/api_formatter.rb @@ -2,43 +2,39 @@ module RspecApiDocumentation class ApiFormatter < RSpec::Core::Formatters::BaseTextFormatter + RSpec::Core::Formatters.register self, :example_passed, :example_failed, :stop + def initialize(output) super output.puts "Generating API Docs" end - def start(example_count) + def start(notification) super RspecApiDocumentation.documentations.each(&:clear_docs) end - def example_group_started(example_group) + def example_group_started(notification) super - output.puts " #{example_group.description}" + output.puts " #{@example_group.description}" end - def example_passed(example) - super - - output.puts " * #{example.description}" + def example_passed(example_notification) + output.puts " * #{example_notification.example.description}" RspecApiDocumentation.documentations.each do |documentation| - documentation.document_example(example) + documentation.document_example(example_notification.example) end end - def example_failed(example) - super - - output.puts " ! #{example.description} (FAILED)" + def example_failed(example_notification) + output.puts " ! #{example_notification.example.description} (FAILED)" end def stop - super - RspecApiDocumentation.documentations.each(&:write) end end diff --git a/lib/rspec_api_documentation/dsl/endpoint.rb b/lib/rspec_api_documentation/dsl/endpoint.rb index f96636cd..c01eb9d7 100644 --- a/lib/rspec_api_documentation/dsl/endpoint.rb +++ b/lib/rspec_api_documentation/dsl/endpoint.rb @@ -12,12 +12,7 @@ module Endpoint module ClassMethods def example_request(description, params = {}, &block) - file_path = caller.first[0, caller.first =~ /:/] - - location = caller.first[0, caller.first =~ /(:in|$)/] - location = relative_path(location) - - example description, :location => location, :file_path => file_path do + example description do do_request(params) instance_eval &block if block_given? end diff --git a/rspec_api_documentation.gemspec b/rspec_api_documentation.gemspec index 6ef735e5..d585daa6 100644 --- a/rspec_api_documentation.gemspec +++ b/rspec_api_documentation.gemspec @@ -14,7 +14,7 @@ Gem::Specification.new do |s| s.required_rubygems_version = ">= 1.3.6" - s.add_runtime_dependency "rspec", "~> 2.99.0.rc1" + s.add_runtime_dependency "rspec", "~> 3.0.0.rc1" s.add_runtime_dependency "activesupport", ">= 3.0.0" s.add_runtime_dependency "i18n", ">= 0.1.0" s.add_runtime_dependency "mustache", ">= 0.99.4" diff --git a/spec/api_documentation_spec.rb b/spec/api_documentation_spec.rb index c085b687..39930cc4 100644 --- a/spec/api_documentation_spec.rb +++ b/spec/api_documentation_spec.rb @@ -15,11 +15,11 @@ test_file = configuration.docs_dir.join("test") FileUtils.mkdir_p configuration.docs_dir FileUtils.touch test_file - FileUtils.stub(:cp_r) + allow(FileUtils).to receive(:cp_r) subject.clear_docs - File.directory?(configuration.docs_dir).should be_truthy - File.exists?(test_file).should be_falsey + expect(File.directory?(configuration.docs_dir)).to be_truthy + expect(File.exists?(test_file)).to be_falsey end end @@ -30,29 +30,29 @@ let!(:wrapped_example) { RspecApiDocumentation::Example.new(example, configuration) } before do - RspecApiDocumentation::Example.stub(:new).and_return(wrapped_example) + allow(RspecApiDocumentation::Example).to receive(:new).and_return(wrapped_example) end it "should create a new wrapped example" do - RspecApiDocumentation::Example.should_receive(:new).with(example, configuration).and_return(wrapped_example) + expect(RspecApiDocumentation::Example).to receive(:new).with(example, configuration).and_return(wrapped_example) documentation.document_example(example) end context "when the given example should be documented" do - before { wrapped_example.stub(:should_document?).and_return(true) } + before { allow(wrapped_example).to receive(:should_document?).and_return(true) } it "should add the wrapped example to the index" do documentation.document_example(example) - documentation.index.examples.should eq([wrapped_example]) + expect(documentation.index.examples).to eq([wrapped_example]) end end context "when the given example should not be documented" do - before { wrapped_example.stub(:should_document?).and_return(false) } + before { allow(wrapped_example).to receive(:should_document?).and_return(false) } it "should not add the wrapped example to the index" do documentation.document_example(example) - documentation.index.examples.should be_empty + expect(documentation.index.examples).to be_empty end end end @@ -68,8 +68,9 @@ class RspecApiDocumentation::Writers::TextileWriter; end end it "should return the classes from format" do - subject.writers.should == [RspecApiDocumentation::Writers::HtmlWriter, RspecApiDocumentation::Writers::JsonWriter, - RspecApiDocumentation::Writers::TextileWriter] + expect(subject.writers).to eq([RspecApiDocumentation::Writers::HtmlWriter, + RspecApiDocumentation::Writers::JsonWriter, + RspecApiDocumentation::Writers::TextileWriter]) end end @@ -79,7 +80,7 @@ class RspecApiDocumentation::Writers::TextileWriter; end end it "should return the classes from format" do - subject.writers.should == [RspecApiDocumentation::Writers::HtmlWriter] + expect(subject.writers).to eq([RspecApiDocumentation::Writers::HtmlWriter]) end end end @@ -90,13 +91,13 @@ class RspecApiDocumentation::Writers::TextileWriter; end let(:textile_writer) { double(:textile_writer) } before do - subject.stub(:writers => [html_writer, json_writer, textile_writer]) + allow(subject).to receive(:writers).and_return([html_writer, json_writer, textile_writer]) end it "should write the docs in each format" do - html_writer.should_receive(:write).with(subject.index, configuration) - json_writer.should_receive(:write).with(subject.index, configuration) - textile_writer.should_receive(:write).with(subject.index, configuration) + expect(html_writer).to receive(:write).with(subject.index, configuration) + expect(json_writer).to receive(:write).with(subject.index, configuration) + expect(textile_writer).to receive(:write).with(subject.index, configuration) subject.write end end diff --git a/spec/api_formatter_spec.rb b/spec/api_formatter_spec.rb index ca5d41e0..bc48b60c 100644 --- a/spec/api_formatter_spec.rb +++ b/spec/api_formatter_spec.rb @@ -11,35 +11,33 @@ before do RspecApiDocumentation.documentations.each do |configuration| - configuration.stub( - :clear_docs => nil, - :document_example => nil, - :write => nil - ) + allow(configuration).to receive(:clear_docs) + allow(configuration).to receive(:document_example) + allow(configuration).to receive(:write) end end it "should clear all docs on start" do RspecApiDocumentation.documentations.each do |configuration| - configuration.should_receive(:clear_docs) + expect(configuration).to receive(:clear_docs) end - formatter.start(0) + formatter.start(RSpec::Core::Notifications::StartNotification.new(0, 0)) end it "should document passing examples" do example = group.example("Ordering a cup of coffee") {} RspecApiDocumentation.documentations.each do |configuration| - configuration.should_receive(:document_example).with(example) + expect(configuration).to receive(:document_example).with(example) end - formatter.example_passed(example) + formatter.example_passed(RSpec::Core::Notifications::ExampleNotification.for(example)) end it "should write the docs on stop" do RspecApiDocumentation.documentations.each do |configuration| - configuration.should_receive(:write) + expect(configuration).to receive(:write) end formatter.stop @@ -47,9 +45,13 @@ end describe "output" do + let(:reporter) { RSpec::Core::Reporter.new(RSpec::Core::Configuration.new) } + before do # don't do any work - RspecApiDocumentation.stub(:documentations).and_return([]) + allow(RspecApiDocumentation).to receive(:documentations).and_return([]) + + reporter.register_listener formatter, :start, :example_group_started, :example_passed, :example_failed, :stop end context "with passing examples" do @@ -60,8 +62,8 @@ end it "should list the generated docs" do - group.run(formatter) - output.string.split($/).should eq([ + group.run(reporter) + expect(output.string.split($/)).to eq([ "Generating API Docs", " Orders", " * Ordering a cup of coffee", @@ -74,13 +76,13 @@ context "with failing examples" do before do group.example("Ordering a cup of coffee") {} - group.example("Updating an order") { true.should eq(false) } - group.example("Viewing an order") { true.should eq(false) } + group.example("Updating an order") { expect(true).to eq(false) } + group.example("Viewing an order") { expect(true).to eq(false) } end it "should indicate failures" do - group.run(formatter) - output.string.split($/).should eq([ + group.run(reporter) + expect(output.string.split($/)).to eq([ "Generating API Docs", " Orders", " * Ordering a cup of coffee", diff --git a/spec/configuration_spec.rb b/spec/configuration_spec.rb index 92ec75fc..87288f47 100644 --- a/spec/configuration_spec.rb +++ b/spec/configuration_spec.rb @@ -13,18 +13,18 @@ describe ".add_setting" do it 'should allow creating a new setting' do RspecApiDocumentation::Configuration.add_setting :new_setting - configuration.should respond_to(:new_setting) - configuration.should respond_to(:new_setting=) + expect(configuration).to respond_to(:new_setting) + expect(configuration).to respond_to(:new_setting=) end it 'should allow setting a default' do RspecApiDocumentation::Configuration.add_setting :new_setting, :default => "default" - configuration.new_setting.should == "default" + expect(configuration.new_setting).to eq("default") end it "should allow the default setting to be a lambda" do RspecApiDocumentation::Configuration.add_setting :another_setting, :default => lambda { |config| config.new_setting } - configuration.another_setting.should == "default" + expect(configuration.another_setting).to eq("default") end end @@ -62,37 +62,37 @@ it "should take a block" do called = false subject.define_group(:foo) { called = true } - called.should eq(true) + expect(called).to eq(true) end it "should yield a sub-configuration" do subject.define_group(:foo) do |config| - config.should be_a(described_class) - config.parent.should equal(subject) + expect(config).to be_a(described_class) + expect(config.parent).to equal(subject) end end it "should set the sub-configuration filter" do subject.define_group(:foo) do |config| - config.filter.should eq(:foo) + expect(config.filter).to eq(:foo) end end it "should inherit its parents configurations" do subject.format = :json subject.define_group(:sub) do |config| - config.format.should == :json + expect(config.format).to eq(:json) end end it "should scope the documentation directory" do subject.define_group(:sub) do |config| - config.docs_dir.should == subject.docs_dir.join('sub') + expect(config.docs_dir).to eq(subject.docs_dir.join('sub')) end end end - it { should be_a(Enumerable) } + it { expect(subject).to be_a(Enumerable) } it "should enumerate through recursively and include self" do configs = [subject] @@ -105,7 +105,7 @@ end end end - subject.to_a.should eq(configs) + expect(subject.to_a).to eq(configs) end describe "#groups" do diff --git a/spec/curl_spec.rb b/spec/curl_spec.rb index 180b79ec..743602a7 100644 --- a/spec/curl_spec.rb +++ b/spec/curl_spec.rb @@ -32,7 +32,7 @@ it { should_not =~ /-H "Cookies: "/ } it "should call post" do - curl.should_receive(:post) + expect(curl).to receive(:post) curl.output(host) end end @@ -59,7 +59,7 @@ it { should_not =~ /-H "Cookies: "/ } it "should call get" do - curl.should_receive(:get) + expect(curl).to receive(:get) curl.output(host) end end @@ -87,7 +87,7 @@ it { should_not =~ /-H "Cookies: "/ } it "should call put" do - curl.should_receive(:put) + expect(curl).to receive(:put) curl.output(host) end end @@ -114,7 +114,7 @@ it { should_not =~ /-H "Cookies: "/ } it "should call delete" do - curl.should_receive(:delete) + expect(curl).to receive(:delete) curl.output(host) end end @@ -141,7 +141,7 @@ it { should_not =~ /-H "Cookies: "/ } it "should call get" do - curl.should_receive(:head) + expect(curl).to receive(:head) curl.output(host) end end @@ -169,7 +169,7 @@ it { should_not =~ /-H "Cookies: "/ } it "should call put" do - curl.should_receive(:patch) + expect(curl).to receive(:patch) curl.output(host) end end diff --git a/spec/dsl_spec.rb b/spec/dsl_spec.rb index 97425886..786ab995 100644 --- a/spec/dsl_spec.rb +++ b/spec/dsl_spec.rb @@ -4,7 +4,7 @@ describe "Non-api documentation specs" do it "should not be polluted by the rspec api dsl" do |example| - example.example_group.should_not include(RspecApiDocumentation::DSL) + expect(example.example_group).to_not include(RspecApiDocumentation::DSL) end end @@ -18,17 +18,17 @@ describe "example context" do it "should provide a client" do - client.should be_a(RspecApiDocumentation::RackTestClient) + expect(client).to be_a(RspecApiDocumentation::RackTestClient) end it "should return the same client every time" do - client.should equal(client) + expect(client).to equal(client) end end [:post, :get, :put, :delete, :head, :patch].each do |http_method| send(http_method, "/path") do - specify { |example| example.example_group.description.should eq("#{http_method.to_s.upcase} /path") } + specify { |example| expect(example.example_group.description).to eq("#{http_method.to_s.upcase} /path") } describe "example metadata" do subject { |example| example.metadata } @@ -45,7 +45,7 @@ describe "do_request" do it "should call the correct method on the client" do - client.should_receive(http_method) + expect(client).to receive(http_method) do_request end end @@ -65,7 +65,7 @@ subject { |example| example.metadata } it "should include the documentated parameters" do - subject[:parameters].should eq( + expect(subject[:parameters]).to eq( [ { :name => "type", :description => "The type of drink you want.", :required => true }, { :name => "size", :description => "The size of drink you want.", :required => true }, @@ -80,7 +80,7 @@ describe "params" do it "should equal the assigned parameter values" do - params.should eq("type" => "coffee", "size" => "medium") + expect(params).to eq("type" => "coffee", "size" => "medium") end end end @@ -101,30 +101,30 @@ let(:raw_post) { { :bill => params }.to_json } it "should send the raw post body" do - client.should_receive(method).with(path, raw_post, nil) + expect(client).to receive(method).with(path, raw_post, nil) do_request end end context "when raw_post is not defined" do it "should send the params hash" do - client.should_receive(method).with(path, params, nil) + expect(client).to receive(method).with(path, params, nil) do_request end end it "should allow extra parameters to be passed in" do - client.should_receive(method).with(path, params.merge("extra" => true), nil) + expect(client).to receive(method).with(path, params.merge("extra" => true), nil) do_request(:extra => true) end it "should overwrite parameters" do - client.should_receive(method).with(path, params.merge("size" => "large"), nil) + expect(client).to receive(method).with(path, params.merge("size" => "large"), nil) do_request(:size => "large") end it "should overwrite path variables" do - client.should_receive(method).with("/orders/2", params, nil) + expect(client).to receive(method).with("/orders/2", params, nil) do_request(:id => 2) end end @@ -134,11 +134,11 @@ example.metadata[:requests] = ["first request"] no_doc do - example.metadata[:requests].should be_empty + expect(example.metadata[:requests]).to be_empty example.metadata[:requests] = ["not documented"] end - example.metadata[:requests].should == ["first request"] + expect(example.metadata[:requests]).to eq(["first request"]) end end end @@ -148,7 +148,7 @@ describe "do_request" do it "should correctly set path variables and other parameters" do - client.should_receive(method).with("/orders/3/line_items/2?type=short", nil, nil) + expect(client).to receive(method).with("/orders/3/line_items/2?type=short", nil, nil) do_request(:id => 2, :order_id => 3, :type => 'short') end end @@ -164,13 +164,13 @@ let(:order_id) { order.id } it "should have the value of id subtituted for :id" do - subject.should eq("/orders/1") + expect(subject).to eq("/orders/1") end end context "when id has not been defined" do it "should be unchanged" do - subject.should eq("/orders/:order_id") + expect(subject).to eq("/orders/:order_id") end end end @@ -180,29 +180,29 @@ parameter :per_page, "Number of results on a page" it "should only have 1 parameter" do |example| - example.metadata[:parameters].length.should == 1 + expect(example.metadata[:parameters].length).to eq(1) end context "another parameter" do parameter :page, "Current page" it 'should have 2 parameters' do |example| - example.metadata[:parameters].length.should == 2 + expect(example.metadata[:parameters].length).to eq(2) end end end callback "Order creation notification callback" do it "should provide a destination" do - destination.should be_a(RspecApiDocumentation::TestServer) + expect(destination).to be_a(RspecApiDocumentation::TestServer) end it "should return the same destination every time" do - destination.should equal(destination) + expect(destination).to equal(destination) end describe "trigger_callback" do - let(:callback_url) { double(:callback_url) } + let(:callback_url) { "callback url" } let(:callbacks_triggered) { [] } trigger_callback do @@ -211,7 +211,7 @@ it "should get called once when do_callback is called" do do_callback - callbacks_triggered.length.should eq(1) + expect(callbacks_triggered.length).to eq(1) end end @@ -228,12 +228,12 @@ it "should mock requests to the callback url to be handled by the destination" do called = false - destination.stub(:call) do + allow(destination).to receive(:call) do called = true [200, {}, []] end do_callback - called.should be_truthy + expect(called).to be_truthy end end @@ -255,11 +255,11 @@ get "/users/:id/orders" do example "Page should be in the query string" do - client.should_receive(method) do |path, data, headers| - path.should =~ /^\/users\/1\/orders\?/ - path.split("?")[1].split("&").sort.should == "page=2&message=Thank+you".split("&").sort - data.should be_nil - headers.should be_nil + expect(client).to receive(method) do |path, data, headers| + expect(path).to match(/^\/users\/1\/orders\?/) + expect(path.split("?")[1].split("&").sort).to eq("page=2&message=Thank+you".split("&").sort) + expect(data).to be_nil + expect(headers).to be_nil end do_request end @@ -267,7 +267,7 @@ post "/users/:id/orders" do example "Page should be in the post body" do - client.should_receive(method).with("/users/1/orders", {"page" => 2, "message" => "Thank you"}, nil) + expect(client).to receive(method).with("/users/1/orders", {"page" => 2, "message" => "Thank you"}, nil) do_request end end @@ -276,14 +276,14 @@ context "#app" do it "should provide access to the configurations app" do - app.should == RspecApiDocumentation.configuration.app + expect(app).to eq(RspecApiDocumentation.configuration.app) end context "defining a new app, in an example" do let(:app) { "Sinatra" } it "should use the user defined app" do - app.should == "Sinatra" + expect(app).to eq("Sinatra") end end end @@ -292,7 +292,7 @@ post "/orders" do example "Creating an order" do |example| explanation "By creating an order..." - example.metadata[:explanation].should == "By creating an order..." + expect(example.metadata[:explanation]).to eq("By creating an order...") end end end @@ -305,8 +305,8 @@ let(:id_eq) { [1, 2] } example "parsed properly" do - client.should_receive(:get) do |path, data, headers| - Rack::Utils.parse_nested_query(path.gsub('/orders?', '')).should eq({"id_eq"=>['1', '2']}) + expect(client).to receive(:get) do |path, data, headers| + expect(Rack::Utils.parse_nested_query(path.gsub('/orders?', ''))).to eq({"id_eq"=>['1', '2']}) end do_request end @@ -318,8 +318,8 @@ let(:within_id) { {"first" => 1, "last" => 10, "exclude" => [3,5,7]} } example "parsed properly" do - client.should_receive(:get) do |path, data, headers| - Rack::Utils.parse_nested_query(path.gsub('/orders?', '')).should eq({ + expect(client).to receive(:get) do |path, data, headers| + expect(Rack::Utils.parse_nested_query(path.gsub('/orders?', ''))).to eq({ "search" => { "within_id" => {"first" => '1', "last" => '10', "exclude" => ['3','5','7']}} }) end @@ -337,7 +337,7 @@ context "no extra params" do before do - client.should_receive(:post).with("/orders", {}, nil) + expect(client).to receive(:post).with("/orders", {}, nil) end example_request "Creating an order" @@ -349,7 +349,7 @@ context "extra options for do_request" do before do - client.should_receive(:post).with("/orders", {"order_type" => "big"}, nil) + expect(client).to receive(:post).with("/orders", {"order_type" => "big"}, nil) end example_request "should take an optional parameter hash", :order_type => "big" @@ -361,7 +361,7 @@ post "/orders" do context "extra options for do_request" do before do - client.should_receive(:post).with("/orders", {"order_type" => "big"}, nil) + expect(client).to receive(:post).with("/orders", {"order_type" => "big"}, nil) end example_request "should take an optional parameter hash", :order_type => "big" @@ -372,13 +372,13 @@ context "last_response helpers" do put "/orders" do it "status" do - client.stub(:last_response).and_return(double(:status => 200)) - status.should == 200 + allow(client).to receive(:last_response).and_return(double(:status => 200)) + expect(status).to eq(200) end it "response_body" do - client.stub(:last_response).and_return(double(:body => "the body")) - response_body.should == "the body" + allow(client).to receive(:last_response).and_return(double(:body => "the body")) + expect(response_body).to eq("the body") end end end @@ -388,7 +388,7 @@ header "Accept", "application/json" it "should be sent with the request" do |example| - example.metadata[:headers].should == { "Accept" => "application/json" } + expect(example.metadata[:headers]).to eq({ "Accept" => "application/json" }) end context "nested headers" do @@ -408,7 +408,7 @@ end it "adds to headers" do - headers.should == { "Accept" => "application/json", "Content-Type" => "application/json" } + expect(headers).to eq({ "Accept" => "application/json", "Content-Type" => "application/json" }) end end end @@ -419,18 +419,18 @@ let(:accept) { "application/json" } it "should be sent with the request" do |example| - example.metadata[:headers].should == { "Accept" => :accept } + expect(example.metadata[:headers]).to eq({ "Accept" => :accept }) end it "should fill out into the headers" do - headers.should == { "Accept" => "application/json" } + expect(headers).to eq({ "Accept" => "application/json" }) end context "nested headers" do header "Content-Type", "application/json" it "does not affect the outer context's assertions" do - headers.should == { "Accept" => "application/json", "Content-Type" => "application/json" } + expect(headers).to eq({ "Accept" => "application/json", "Content-Type" => "application/json" }) end end @@ -438,7 +438,7 @@ header "X-My-Header", :my_header it "should not be in the headers hash" do - headers.should == { "Accept" => "application/json" } + expect(headers).to eq({ "Accept" => "application/json" }) end end end @@ -449,12 +449,12 @@ parameter :page, "Current page" it 'should have 1 parameter' do |example| - example.metadata[:parameters].length.should == 1 + expect(example.metadata[:parameters].length).to eq(1) end end resource "passing in document to resource", :document => :not_all do it "should have the correct tag" do |example| - example.metadata[:document].should == :not_all + expect(example.metadata[:document]).to eq(:not_all) end end diff --git a/spec/example_spec.rb b/spec/example_spec.rb index fc2d97eb..358eccdf 100644 --- a/spec/example_spec.rb +++ b/spec/example_spec.rb @@ -18,8 +18,8 @@ let(:metadata) {{ :foo => nil }} it "should return the metadata value for the given method selector as a key" do - example.should respond_to(:foo) - example.foo.should eq(nil) + expect(example).to respond_to(:foo) + expect(example.foo).to eq(nil) end end @@ -27,8 +27,8 @@ before { metadata.delete(:foo) } it "should delegate the method to the example" do - rspec_example.should_receive(:foo).with(:bar, :baz) - example.should respond_to(:foo) + expect(rspec_example).to receive(:foo).with(:bar, :baz) + expect(example).to respond_to(:foo) example.foo(:bar, :baz) end end @@ -38,7 +38,7 @@ let(:metadata) {{ :method => "GET" }} it "should return what is in the metadata" do - example.http_method.should == "GET" + expect(example.http_method).to eq("GET") end end @@ -133,13 +133,13 @@ subject { example.has_parameters? } context "when parameters are defined" do - before { example.stub(:parameters).and_return([double]) } + before { allow(example).to receive(:parameters).and_return([double]) } it { should be_truthy } end context "when parameters are empty" do - before { example.stub(:parameters).and_return([]) } + before { allow(example).to receive(:parameters).and_return([]) } it { should be_falsey } end @@ -152,11 +152,11 @@ describe "#explanation" do it "should return the metadata explanation" do example.metadata[:explanation] = "Here is an explanation" - example.explanation.should == "Here is an explanation" + expect(example.explanation).to eq("Here is an explanation") end it "should return an empty string when not set" do - example.explanation.should == nil + expect(example.explanation).to eq(nil) end end @@ -182,7 +182,7 @@ end it "should filter out anything not explicitly mentioned" do - subject.requests.should == [ + expect(subject.requests).to eq([ { :request_headers => { "Included" => "data", @@ -195,7 +195,7 @@ }, :request_method => "GET" } - ] + ]) end end @@ -221,7 +221,7 @@ end it "should filter out anything not explicitly mentioned" do - subject.requests.should == [ + expect(subject.requests).to eq([ { :response_headers => { "Included" => "data", @@ -234,7 +234,7 @@ }, :request_method => "GET" } - ] + ]) end end end diff --git a/spec/index_spec.rb b/spec/index_spec.rb index bb28d9ac..1b6fe66a 100644 --- a/spec/index_spec.rb +++ b/spec/index_spec.rb @@ -13,7 +13,7 @@ end it "should contain all added examples" do - index.examples.should eq(examples) + expect(index.examples).to eq(examples) end end end diff --git a/spec/rack_test_client_spec.rb b/spec/rack_test_client_spec.rb index d3ce5b28..6849a0c1 100644 --- a/spec/rack_test_client_spec.rb +++ b/spec/rack_test_client_spec.rb @@ -35,7 +35,7 @@ class StubApp < Sinatra::Base subject { test_client } - it { should be_a(RspecApiDocumentation::RackTestClient) } + it { expect(subject).to be_a(RspecApiDocumentation::RackTestClient) } its(:context) { should equal(context) } its(:example) { |example| should equal(example) } @@ -47,11 +47,11 @@ class StubApp < Sinatra::Base end it "should handle xml data" do - test_client.response_headers["Content-Type"].should =~ /application\/xml/ + expect(test_client.response_headers["Content-Type"]).to match(/application\/xml/) end it "should log the request" do |example| - example.metadata[:requests].first[:response_body].should be_present + expect(example.metadata[:requests].first[:response_body]).to be_present end end @@ -61,7 +61,7 @@ class StubApp < Sinatra::Base end it 'should contain the query_string' do - test_client.query_string.should == "query_string=true" + expect(test_client.query_string).to eq("query_string=true") end end @@ -71,7 +71,7 @@ class StubApp < Sinatra::Base end it "should contain all the headers" do - test_client.request_headers.should eq({ + expect(test_client.request_headers).to eq({ "Accept" => "application/json", "Content-Type" => "application/json", "Host" => "example.org", @@ -88,7 +88,7 @@ class StubApp < Sinatra::Base context "when examples should be documented", :document => true do it "should still argument the metadata" do |example| metadata = example.metadata[:requests].first - metadata[:request_query_parameters].should == {'query' => nil, 'other' => 'exists'} + expect(metadata[:request_query_parameters]).to eq({'query' => nil, 'other' => 'exists'}) end end end @@ -104,19 +104,19 @@ class StubApp < Sinatra::Base context "when examples should be documented", :document => true do it "should augment the metadata with information about the request" do |example| metadata = example.metadata[:requests].first - metadata[:request_method].should eq("POST") - metadata[:request_path].should eq("/greet?query=test+query") - metadata[:request_body].should be_present - metadata[:request_headers].should include({'Content-Type' => 'application/json;charset=utf-8'}) - metadata[:request_headers].should include({'X-Custom-Header' => 'custom header value'}) - metadata[:request_query_parameters].should == {"query" => "test query"} - metadata[:request_content_type].should match(/application\/json/) - metadata[:response_status].should eq(200) - metadata[:response_body].should be_present - metadata[:response_headers]['Content-Type'].should match(/application\/json/) - metadata[:response_headers]['Content-Length'].should == '17' - metadata[:response_content_type].should match(/application\/json/) - metadata[:curl].should eq(RspecApiDocumentation::Curl.new("POST", "/greet?query=test+query", post_data, {"Content-Type" => "application/json;charset=utf-8", "X-Custom-Header" => "custom header value", "Host" => "example.org", "Cookie" => ""})) + expect(metadata[:request_method]).to eq("POST") + expect(metadata[:request_path]).to eq("/greet?query=test+query") + expect(metadata[:request_body]).to be_present + expect(metadata[:request_headers]).to include({'Content-Type' => 'application/json;charset=utf-8'}) + expect(metadata[:request_headers]).to include({'X-Custom-Header' => 'custom header value'}) + expect(metadata[:request_query_parameters]).to eq({"query" => "test query"}) + expect(metadata[:request_content_type]).to match(/application\/json/) + expect(metadata[:response_status]).to eq(200) + expect(metadata[:response_body]).to be_present + expect(metadata[:response_headers]['Content-Type']).to match(/application\/json/) + expect(metadata[:response_headers]['Content-Length']).to eq('17') + expect(metadata[:response_content_type]).to match(/application\/json/) + expect(metadata[:curl]).to eq(RspecApiDocumentation::Curl.new("POST", "/greet?query=test+query", post_data, {"Content-Type" => "application/json;charset=utf-8", "X-Custom-Header" => "custom header value", "Host" => "example.org", "Cookie" => ""})) end context "when post data is not json" do @@ -124,8 +124,8 @@ class StubApp < Sinatra::Base it "should not nil out request_body" do |example| body = example.metadata[:requests].first[:request_body] - body.should =~ /target=nurse/ - body.should =~ /email=email%40example\.com/ + expect(body).to match(/target=nurse/) + expect(body).to match(/email=email%40example\.com/) end end @@ -133,7 +133,7 @@ class StubApp < Sinatra::Base let(:post_data) { } it "should nil out request_body" do |example| - example.metadata[:requests].first[:request_body].should be_nil + expect(example.metadata[:requests].first[:request_body]).to be_nil end end end diff --git a/spec/rspec_api_documentation_spec.rb b/spec/rspec_api_documentation_spec.rb index 378e8a89..627477f9 100644 --- a/spec/rspec_api_documentation_spec.rb +++ b/spec/rspec_api_documentation_spec.rb @@ -3,11 +3,11 @@ describe RspecApiDocumentation do describe "#configuration" do it "should be a configuration" do - RspecApiDocumentation.configuration.should be_a(RspecApiDocumentation::Configuration) + expect(RspecApiDocumentation.configuration).to be_a(RspecApiDocumentation::Configuration) end it "returns the same configuration every time" do - RspecApiDocumentation.configuration.should equal(RspecApiDocumentation.configuration) + expect(RspecApiDocumentation.configuration).to equal(RspecApiDocumentation.configuration) end end @@ -15,11 +15,11 @@ let(:configuration) { double(:confiugration) } before do - RspecApiDocumentation.stub(:configuration).and_return(configuration) + allow(RspecApiDocumentation).to receive(:configuration).and_return(configuration) end it "should yield the configuration to the block" do - configuration.should_receive(:foo) + allow(configuration).to receive(:foo) RspecApiDocumentation.configure do |config| config.foo end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 0e1b027e..8cb67272 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,6 +1,7 @@ require 'rspec_api_documentation' require 'fakefs/spec_helpers' require 'rspec/its' +require 'pry' RSpec.configure do |config| config.include FakeFS::SpecHelpers diff --git a/spec/views/html_example_spec.rb b/spec/views/html_example_spec.rb index 98c85d8c..804101de 100644 --- a/spec/views/html_example_spec.rb +++ b/spec/views/html_example_spec.rb @@ -9,7 +9,7 @@ let(:html_example) { described_class.new(example, configuration) } it "should have downcased filename" do - html_example.filename.should == "ordering_a_cup_of_coffee.html" + expect(html_example.filename).to eq("ordering_a_cup_of_coffee.html") end describe "multi charctor example name" do @@ -18,7 +18,7 @@ it "should have downcased filename" do filename = Digest::MD5.new.update(label).to_s - html_example.filename.should == filename + ".html" + expect(html_example.filename).to eq(filename + ".html") end end end diff --git a/spec/writers/combined_text_example_spec.rb b/spec/writers/combined_text_example_spec.rb index fde1e537..e681b965 100644 --- a/spec/writers/combined_text_example_spec.rb +++ b/spec/writers/combined_text_example_spec.rb @@ -7,11 +7,11 @@ let(:example) { RspecApiDocumentation::Writers::CombinedTextExample.new(rspec_example) } it "should format its resource name" do - example.resource_name.should == "foo_bar" + expect(example.resource_name).to eq("foo_bar") end it "should format its description" do - example.description.should == "ABCDEFG\n-------\n\n" + expect(example.description).to eq("ABCDEFG\n-------\n\n") end context "given parameters" do @@ -23,12 +23,12 @@ }} it "should format its parameters" do - example.parameters.should == "Parameters:\n * foo - Foo!\n * bar - Bar!\n\n" + expect(example.parameters).to eq("Parameters:\n * foo - Foo!\n * bar - Bar!\n\n") end end it "renders nothing if there are no parameters" do - example.parameters.should == "" + expect(example.parameters).to eq("") end context "when there are requests" do @@ -63,15 +63,15 @@ let(:metadata) {{ :requests => requests }} it "exposes the requests" do - example.requests.should == [ + expect(example.requests).to eq([ [" GET /greetings\n Header: value\n\n baz=quux\n foo=bar\n", " Status: 200 OK\n Foo: bar\n Header: value\n\n body\n"], [" POST /greetings\n\n body\n", " Status: 404 Not Found\n Header: value\n\n body\n"], [" DELETE /greetings/1\n", " Status: 200 OK\n Header: value\n"], - ] + ]) end end it "returns the empty array if there are no requests" do - example.requests.should == [] + expect(example.requests).to eq([]) end end diff --git a/spec/writers/html_writer_spec.rb b/spec/writers/html_writer_spec.rb index de3401d1..7c30d0ac 100644 --- a/spec/writers/html_writer_spec.rb +++ b/spec/writers/html_writer_spec.rb @@ -9,8 +9,8 @@ let(:writer) { double(:writer) } it "should build a new writer and write the docs" do - described_class.stub(:new).with(index, configuration).and_return(writer) - writer.should_receive(:write) + allow(described_class).to receive(:new).with(index, configuration).and_return(writer) + expect(writer).to receive(:write) described_class.write(index, configuration) end end @@ -28,7 +28,7 @@ it "should write the index" do writer.write index_file = File.join(configuration.docs_dir, "index.html") - File.exists?(index_file).should be_truthy + expect(File.exists?(index_file)).to be_truthy end end end diff --git a/spec/writers/index_helper_spec.rb b/spec/writers/index_helper_spec.rb index 66c48867..2b39c929 100644 --- a/spec/writers/index_helper_spec.rb +++ b/spec/writers/index_helper_spec.rb @@ -12,11 +12,11 @@ subject { RspecApiDocumentation::Writers::IndexHelper.sections(examples, configuration) } it "should order resources by resource name" do - subject.map { |resource| resource[:resource_name] }.should == ["Cart", "Order"] + expect(subject.map { |resource| resource[:resource_name] }).to eq(["Cart", "Order"]) end it "should order examples by description" do - subject.detect { |resource| resource[:resource_name] == "Order"}[:examples].should == [example_2, example_1] + expect(subject.detect { |resource| resource[:resource_name] == "Order"}[:examples]).to eq([example_2, example_1]) end end @@ -24,11 +24,11 @@ subject { RspecApiDocumentation::Writers::IndexHelper.sections(examples, double(:keep_source_order => true)) } it "should order resources by source code declaration" do - subject.map { |resource| resource[:resource_name] }.should == ["Order", "Cart"] + expect(subject.map { |resource| resource[:resource_name] }).to eq(["Order", "Cart"]) end it "should order examples by source code declaration" do - subject.detect { |resource| resource[:resource_name] == "Order"}[:examples].should == [example_1, example_2] + expect(subject.detect { |resource| resource[:resource_name] == "Order"}[:examples]).to eq([example_1, example_2]) end end end diff --git a/spec/writers/json_iodocs_writer_spec.rb b/spec/writers/json_iodocs_writer_spec.rb index 44b61c5b..bfee639c 100644 --- a/spec/writers/json_iodocs_writer_spec.rb +++ b/spec/writers/json_iodocs_writer_spec.rb @@ -8,8 +8,8 @@ let(:writer) { double(:writer) } it "should build a new writer and write the docs" do - described_class.stub(:new).with(index, configuration).and_return(writer) - writer.should_receive(:write) + allow(described_class).to receive(:new).with(index, configuration).and_return(writer) + expect(writer).to receive(:write) described_class.write(index, configuration) end end @@ -18,14 +18,14 @@ let(:writer) { described_class.new(index, configuration) } before do - configuration.api_name.stub(:parameterize => "Name") + allow(configuration.api_name).to receive(:parameterize).and_return("Name") FileUtils.mkdir_p(configuration.docs_dir) end it "should write the index" do writer.write index_file = File.join(configuration.docs_dir, "apiconfig.json") - File.exists?(index_file).should be_truthy + expect(File.exists?(index_file)).to be_truthy end end end diff --git a/spec/writers/json_writer_spec.rb b/spec/writers/json_writer_spec.rb index e7e09d79..4f36d506 100644 --- a/spec/writers/json_writer_spec.rb +++ b/spec/writers/json_writer_spec.rb @@ -8,8 +8,8 @@ let(:writer) { double(:writer) } it "should build a new writer and write the docs" do - described_class.stub(:new).with(index, configuration).and_return(writer) - writer.should_receive(:write) + allow(described_class).to receive(:new).with(index, configuration).and_return(writer) + expect(writer).to receive(:write) described_class.write(index, configuration) end end @@ -24,7 +24,7 @@ it "should write the index" do writer.write index_file = File.join(configuration.docs_dir, "index.json") - File.exists?(index_file).should be_truthy + expect(File.exists?(index_file)).to be_truthy end end end diff --git a/spec/writers/markdown_writer_spec.rb b/spec/writers/markdown_writer_spec.rb index 9f40aa4b..e7612c18 100644 --- a/spec/writers/markdown_writer_spec.rb +++ b/spec/writers/markdown_writer_spec.rb @@ -9,8 +9,8 @@ let(:writer) { double(:writer) } it "should build a new writer and write the docs" do - described_class.stub(:new).with(index, configuration).and_return(writer) - writer.should_receive(:write) + allow(described_class).to receive(:new).with(index, configuration).and_return(writer) + expect(writer).to receive(:write) described_class.write(index, configuration) end end @@ -28,8 +28,7 @@ it "should write the index" do writer.write index_file = File.join(configuration.docs_dir, "index.markdown") - File.exists?(index_file).should be_truthy + expect(File.exists?(index_file)).to be_truthy end end - end diff --git a/spec/writers/textile_writer_spec.rb b/spec/writers/textile_writer_spec.rb index 27d4474c..1190695d 100644 --- a/spec/writers/textile_writer_spec.rb +++ b/spec/writers/textile_writer_spec.rb @@ -9,8 +9,8 @@ let(:writer) { double(:writer) } it "should build a new writer and write the docs" do - described_class.stub(:new).with(index, configuration).and_return(writer) - writer.should_receive(:write) + allow(described_class).to receive(:new).with(index, configuration).and_return(writer) + expect(writer).to receive(:write) described_class.write(index, configuration) end end @@ -28,8 +28,7 @@ it "should write the index" do writer.write index_file = File.join(configuration.docs_dir, "index.textile") - File.exists?(index_file).should be_truthy + expect(File.exists?(index_file)).to be_truthy end end - end From 831900b12d10c911e10172c9721bedb04db1dd28 Mon Sep 17 00:00:00 2001 From: Eric Oestrich Date: Wed, 21 May 2014 17:39:40 -0400 Subject: [PATCH 04/10] Move the example method out of private RSpec doesn't like that it's private in a real app. --- lib/rspec_api_documentation/dsl/endpoint.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/rspec_api_documentation/dsl/endpoint.rb b/lib/rspec_api_documentation/dsl/endpoint.rb index c01eb9d7..6552e970 100644 --- a/lib/rspec_api_documentation/dsl/endpoint.rb +++ b/lib/rspec_api_documentation/dsl/endpoint.rb @@ -101,12 +101,12 @@ def explanation(text) example.metadata[:explanation] = text end - private - def example RSpec.current_example end + private + def rspec_api_documentation_client send(RspecApiDocumentation.configuration.client_method) end From bacdea66e709e8d5d195ad577844991a99c77d86 Mon Sep 17 00:00:00 2001 From: Eric Oestrich Date: Thu, 22 May 2014 17:41:54 -0400 Subject: [PATCH 05/10] Formatters #stop receives a NullNotification --- lib/rspec_api_documentation/api_formatter.rb | 2 +- spec/api_formatter_spec.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/rspec_api_documentation/api_formatter.rb b/lib/rspec_api_documentation/api_formatter.rb index d931c09d..7a9f97f3 100644 --- a/lib/rspec_api_documentation/api_formatter.rb +++ b/lib/rspec_api_documentation/api_formatter.rb @@ -34,7 +34,7 @@ def example_failed(example_notification) output.puts " ! #{example_notification.example.description} (FAILED)" end - def stop + def stop(notification) RspecApiDocumentation.documentations.each(&:write) end end diff --git a/spec/api_formatter_spec.rb b/spec/api_formatter_spec.rb index bc48b60c..266cad8d 100644 --- a/spec/api_formatter_spec.rb +++ b/spec/api_formatter_spec.rb @@ -40,7 +40,7 @@ expect(configuration).to receive(:write) end - formatter.stop + formatter.stop(RSpec::Core::Notifications::NullNotification.new) end end From df1e45a94061c084fd712afe94c122db87036842 Mon Sep 17 00:00:00 2001 From: Eric Oestrich Date: Thu, 22 May 2014 17:51:38 -0400 Subject: [PATCH 06/10] Update Callbacks to latest RSpec --- features/callbacks.feature | 8 ++++---- lib/rspec_api_documentation/dsl/callback.rb | 2 +- lib/rspec_api_documentation/test_server.rb | 3 +-- 3 files changed, 6 insertions(+), 7 deletions(-) diff --git a/features/callbacks.feature b/features/callbacks.feature index c6b346cf..3135b9e1 100644 --- a/features/callbacks.feature +++ b/features/callbacks.feature @@ -30,10 +30,10 @@ Feature: Document callbacks example "Receiving a callback when interesting things happen" do do_callback - request_method.should eq("POST") - request_headers["Content-Type"].should eq("application/json") - request_headers["User-Agent"].should eq("InterestingThingApp") - request_body.should eq('{"message":"Something interesting happened!"}') + expect(request_method).to eq("POST") + expect(request_headers["Content-Type"]).to eq("application/json") + expect(request_headers["User-Agent"]).to eq("InterestingThingApp") + expect(request_body).to eq('{"message":"Something interesting happened!"}') end end end diff --git a/lib/rspec_api_documentation/dsl/callback.rb b/lib/rspec_api_documentation/dsl/callback.rb index f4fbd654..1e03c0d0 100644 --- a/lib/rspec_api_documentation/dsl/callback.rb +++ b/lib/rspec_api_documentation/dsl/callback.rb @@ -16,7 +16,7 @@ def trigger_callback(&block) end def destination - @destination ||= RspecApiDocumentation::TestServer.new(self) + @destination ||= RspecApiDocumentation::TestServer.new(RSpec.current_example) end def callback_url diff --git a/lib/rspec_api_documentation/test_server.rb b/lib/rspec_api_documentation/test_server.rb index f80a3f73..9a89ba0b 100644 --- a/lib/rspec_api_documentation/test_server.rb +++ b/lib/rspec_api_documentation/test_server.rb @@ -1,8 +1,7 @@ module RspecApiDocumentation - class TestServer < Struct.new(:context) + class TestServer < Struct.new(:example) include Headers - delegate :example, :to => :context delegate :metadata, :to => :example attr_reader :request_method, :request_headers, :request_body From adf1831e7dd901fcc3ab141d0016f31ada550748 Mon Sep 17 00:00:00 2001 From: Eric Oestrich Date: Thu, 22 May 2014 17:51:48 -0400 Subject: [PATCH 07/10] Last cucumber failure Still failing because of example_request not being able to set the file and location metadata --- features/example_request.feature | 9 +++------ lib/rspec_api_documentation/dsl.rb | 2 +- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/features/example_request.feature b/features/example_request.feature index 7680c1ed..ed2a636d 100644 --- a/features/example_request.feature +++ b/features/example_request.feature @@ -20,7 +20,7 @@ Feature: Example Request resource "Example Request" do get "/" do example_request "Greeting your favorite gem" do - status.should eq(201) + expect(status).to eq(201) end end end @@ -28,11 +28,8 @@ Feature: Example Request 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: - """ - Failure/Error: status.should eq(201) - """ - Then the output should not contain "dsl.rb" + 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 diff --git a/lib/rspec_api_documentation/dsl.rb b/lib/rspec_api_documentation/dsl.rb index 6e74456e..574cabb5 100644 --- a/lib/rspec_api_documentation/dsl.rb +++ b/lib/rspec_api_documentation/dsl.rb @@ -24,4 +24,4 @@ def self.resource(*args, &block) 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 -RSpec.configuration.backtrace_exclusion_patterns << %r{lib/rspec_api_documentation/dsl\.rb} +RSpec.configuration.backtrace_exclusion_patterns << %r{lib/rspec_api_documentation/dsl/} From a4f3f1a4b7628c20f0bae42671f312c6647b4c1e Mon Sep 17 00:00:00 2001 From: Eric Oestrich Date: Fri, 23 May 2014 11:03:48 -0400 Subject: [PATCH 08/10] Add back file locations to example_request --- lib/rspec_api_documentation/dsl/endpoint.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/rspec_api_documentation/dsl/endpoint.rb b/lib/rspec_api_documentation/dsl/endpoint.rb index 6552e970..da856cc8 100644 --- a/lib/rspec_api_documentation/dsl/endpoint.rb +++ b/lib/rspec_api_documentation/dsl/endpoint.rb @@ -12,7 +12,7 @@ module Endpoint module ClassMethods def example_request(description, params = {}, &block) - example description do + example description, :caller => block.send(:caller) do do_request(params) instance_eval &block if block_given? end From 96c8747ce8109fd0e0cfbecd30b18a591dde9b53 Mon Sep 17 00:00:00 2001 From: Eric Oestrich Date: Tue, 10 Jun 2014 12:02:37 -0400 Subject: [PATCH 09/10] Update to full rspec 3 --- Gemfile.lock | 24 ++++++++++++------------ rspec_api_documentation.gemspec | 2 +- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 2af90ed8..6a5e8a9a 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -6,7 +6,7 @@ PATH i18n (>= 0.1.0) json (>= 1.4.6) mustache (>= 0.99.4) - rspec (~> 3.0.0.rc1) + rspec (~> 3.0.0) GEM remote: http://rubygems.org/ @@ -80,21 +80,21 @@ GEM rack-test (0.6.2) rack (>= 1.0) rake (10.1.0) - rspec (3.0.0.rc1) - rspec-core (= 3.0.0.rc1) - rspec-expectations (= 3.0.0.rc1) - rspec-mocks (= 3.0.0.rc1) - rspec-core (3.0.0.rc1) - rspec-support (= 3.0.0.rc1) - rspec-expectations (3.0.0.rc1) + rspec (3.0.0) + rspec-core (~> 3.0.0) + rspec-expectations (~> 3.0.0) + rspec-mocks (~> 3.0.0) + rspec-core (3.0.0) + rspec-support (~> 3.0.0) + rspec-expectations (3.0.0) diff-lcs (>= 1.2.0, < 2.0) - rspec-support (= 3.0.0.rc1) + rspec-support (~> 3.0.0) rspec-its (1.0.1) rspec-core (>= 2.99.0.beta1) rspec-expectations (>= 2.99.0.beta1) - rspec-mocks (3.0.0.rc1) - rspec-support (= 3.0.0.rc1) - rspec-support (3.0.0.rc1) + rspec-mocks (3.0.1) + rspec-support (~> 3.0.0) + rspec-support (3.0.0) safe_yaml (0.9.7) sinatra (1.4.4) rack (~> 1.4) diff --git a/rspec_api_documentation.gemspec b/rspec_api_documentation.gemspec index d585daa6..c39d682b 100644 --- a/rspec_api_documentation.gemspec +++ b/rspec_api_documentation.gemspec @@ -14,7 +14,7 @@ Gem::Specification.new do |s| s.required_rubygems_version = ">= 1.3.6" - s.add_runtime_dependency "rspec", "~> 3.0.0.rc1" + s.add_runtime_dependency "rspec", "~> 3.0.0" s.add_runtime_dependency "activesupport", ">= 3.0.0" s.add_runtime_dependency "i18n", ">= 0.1.0" s.add_runtime_dependency "mustache", ">= 0.99.4" From 9d9113a54db017c1ac6e883ddcc5470a1dfdfb93 Mon Sep 17 00:00:00 2001 From: Eric Oestrich Date: Tue, 10 Jun 2014 12:26:23 -0400 Subject: [PATCH 10/10] Bump version to pre release --- Gemfile.lock | 2 +- rspec_api_documentation.gemspec | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 6a5e8a9a..2f4a8976 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - rspec_api_documentation (3.0.0) + rspec_api_documentation (4.0.0.pre) activesupport (>= 3.0.0) i18n (>= 0.1.0) json (>= 1.4.6) diff --git a/rspec_api_documentation.gemspec b/rspec_api_documentation.gemspec index e4d6d8f6..3a48db58 100644 --- a/rspec_api_documentation.gemspec +++ b/rspec_api_documentation.gemspec @@ -3,7 +3,7 @@ $:.unshift lib unless $:.include?(lib) Gem::Specification.new do |s| s.name = "rspec_api_documentation" - s.version = "3.1.0" + s.version = "4.0.0.pre" s.platform = Gem::Platform::RUBY s.authors = ["Chris Cahoon", "Sam Goldman", "Eric Oestrich"] s.email = ["chris@smartlogicsolutions.com", "sam@smartlogicsolutions.com", "eric@smartlogicsolutions.com"]