Skip to content

Rspec 3 #133

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Jun 10, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 18 additions & 10 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
PATH
remote: .
specs:
rspec_api_documentation (3.1.0)
rspec_api_documentation (4.0.0.pre)
activesupport (>= 3.0.0)
i18n (>= 0.1.0)
json (>= 1.4.6)
mustache (>= 0.99.4)
rspec (~> 2.14.0, >= 2.14.0)
rspec (~> 3.0.0)

GEM
remote: http://rubygems.org/
Expand Down Expand Up @@ -80,14 +80,21 @@ 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)
diff-lcs (>= 1.1.3, < 2.0)
rspec-mocks (2.14.6)
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)
rspec-its (1.0.1)
rspec-core (>= 2.99.0.beta1)
rspec-expectations (>= 2.99.0.beta1)
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)
Expand Down Expand Up @@ -120,6 +127,7 @@ DEPENDENCIES
rack-oauth2 (>= 0.14.4)
rack-test (>= 0.6.2)
rake
rspec-its
rspec_api_documentation!
sinatra
webmock (>= 1.7.0)
8 changes: 4 additions & 4 deletions features/callbacks.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 3 additions & 6 deletions features/example_request.feature
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,16 @@ 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
"""
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
Expand Down
26 changes: 11 additions & 15 deletions lib/rspec_api_documentation/api_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

def stop(notification)
RspecApiDocumentation.documentations.each(&:write)
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/rspec_api_documentation/dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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/}
2 changes: 1 addition & 1 deletion lib/rspec_api_documentation/dsl/callback.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 5 additions & 6 deletions lib/rspec_api_documentation/dsl/endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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, :caller => block.send(:caller) do
do_request(params)
instance_eval &block if block_given?
end
Expand Down Expand Up @@ -106,6 +101,10 @@ def explanation(text)
example.metadata[:explanation] = text
end

def example
RSpec.current_example
end

private

def rspec_api_documentation_client
Expand Down
3 changes: 1 addition & 2 deletions lib/rspec_api_documentation/test_server.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
5 changes: 3 additions & 2 deletions rspec_api_documentation.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ["[email protected]", "[email protected]", "[email protected]"]
Expand All @@ -14,7 +14,7 @@ Gem::Specification.new do |s|

s.required_rubygems_version = ">= 1.3.6"

s.add_runtime_dependency "rspec", "~> 2.14.0", ">= 2.14.0"
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"
Expand All @@ -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'
Expand Down
33 changes: 17 additions & 16 deletions spec/api_documentation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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_true
File.exists?(test_file).should be_false
expect(File.directory?(configuration.docs_dir)).to be_truthy
expect(File.exists?(test_file)).to be_falsey
end
end

Expand All @@ -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
Expand All @@ -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

Expand All @@ -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
Expand All @@ -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
Expand Down
Loading