Skip to content

Commit e8cbdac

Browse files
committed
Merge pull request #133 from zipmark/rspec-3
Rspec 3
2 parents 0715cc5 + 9d9113a commit e8cbdac

27 files changed

+263
-260
lines changed

Gemfile.lock

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
PATH
22
remote: .
33
specs:
4-
rspec_api_documentation (3.1.0)
4+
rspec_api_documentation (4.0.0.pre)
55
activesupport (>= 3.0.0)
66
i18n (>= 0.1.0)
77
json (>= 1.4.6)
88
mustache (>= 0.99.4)
9-
rspec (~> 2.14.0, >= 2.14.0)
9+
rspec (~> 3.0.0)
1010

1111
GEM
1212
remote: http://rubygems.org/
@@ -80,14 +80,21 @@ GEM
8080
rack-test (0.6.2)
8181
rack (>= 1.0)
8282
rake (10.1.0)
83-
rspec (2.14.1)
84-
rspec-core (~> 2.14.0)
85-
rspec-expectations (~> 2.14.0)
86-
rspec-mocks (~> 2.14.0)
87-
rspec-core (2.14.8)
88-
rspec-expectations (2.14.5)
89-
diff-lcs (>= 1.1.3, < 2.0)
90-
rspec-mocks (2.14.6)
83+
rspec (3.0.0)
84+
rspec-core (~> 3.0.0)
85+
rspec-expectations (~> 3.0.0)
86+
rspec-mocks (~> 3.0.0)
87+
rspec-core (3.0.0)
88+
rspec-support (~> 3.0.0)
89+
rspec-expectations (3.0.0)
90+
diff-lcs (>= 1.2.0, < 2.0)
91+
rspec-support (~> 3.0.0)
92+
rspec-its (1.0.1)
93+
rspec-core (>= 2.99.0.beta1)
94+
rspec-expectations (>= 2.99.0.beta1)
95+
rspec-mocks (3.0.1)
96+
rspec-support (~> 3.0.0)
97+
rspec-support (3.0.0)
9198
safe_yaml (0.9.7)
9299
sinatra (1.4.4)
93100
rack (~> 1.4)
@@ -120,6 +127,7 @@ DEPENDENCIES
120127
rack-oauth2 (>= 0.14.4)
121128
rack-test (>= 0.6.2)
122129
rake
130+
rspec-its
123131
rspec_api_documentation!
124132
sinatra
125133
webmock (>= 1.7.0)

features/callbacks.feature

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ Feature: Document callbacks
3030
3131
example "Receiving a callback when interesting things happen" do
3232
do_callback
33-
request_method.should eq("POST")
34-
request_headers["Content-Type"].should eq("application/json")
35-
request_headers["User-Agent"].should eq("InterestingThingApp")
36-
request_body.should eq('{"message":"Something interesting happened!"}')
33+
expect(request_method).to eq("POST")
34+
expect(request_headers["Content-Type"]).to eq("application/json")
35+
expect(request_headers["User-Agent"]).to eq("InterestingThingApp")
36+
expect(request_body).to eq('{"message":"Something interesting happened!"}')
3737
end
3838
end
3939
end

features/example_request.feature

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,16 @@ Feature: Example Request
2020
resource "Example Request" do
2121
get "/" do
2222
example_request "Greeting your favorite gem" do
23-
status.should eq(201)
23+
expect(status).to eq(201)
2424
end
2525
end
2626
end
2727
"""
2828
When I run `rspec app_spec.rb --require ./app.rb --format RspecApiDocumentation::ApiFormatter`
2929

3030
Scenario: Output should have the correct error line
31-
Then the output should contain:
32-
"""
33-
Failure/Error: status.should eq(201)
34-
"""
35-
Then the output should not contain "dsl.rb"
31+
Then the output should contain "expected: 201"
32+
Then the output should not contain "endpoint.rb"
3633
Then the output should contain:
3734
"""
3835
rspec ./app_spec.rb:10 # Example Request GET / Greeting your favorite gem

lib/rspec_api_documentation/api_formatter.rb

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,43 +2,39 @@
22

33
module RspecApiDocumentation
44
class ApiFormatter < RSpec::Core::Formatters::BaseTextFormatter
5+
RSpec::Core::Formatters.register self, :example_passed, :example_failed, :stop
6+
57
def initialize(output)
68
super
79

810
output.puts "Generating API Docs"
911
end
1012

11-
def start(example_count)
13+
def start(notification)
1214
super
1315

1416
RspecApiDocumentation.documentations.each(&:clear_docs)
1517
end
1618

17-
def example_group_started(example_group)
19+
def example_group_started(notification)
1820
super
1921

20-
output.puts " #{example_group.description}"
22+
output.puts " #{@example_group.description}"
2123
end
2224

23-
def example_passed(example)
24-
super
25-
26-
output.puts " * #{example.description}"
25+
def example_passed(example_notification)
26+
output.puts " * #{example_notification.example.description}"
2727

2828
RspecApiDocumentation.documentations.each do |documentation|
29-
documentation.document_example(example)
29+
documentation.document_example(example_notification.example)
3030
end
3131
end
3232

33-
def example_failed(example)
34-
super
35-
36-
output.puts " ! #{example.description} (FAILED)"
33+
def example_failed(example_notification)
34+
output.puts " ! #{example_notification.example.description} (FAILED)"
3735
end
3836

39-
def stop
40-
super
41-
37+
def stop(notification)
4238
RspecApiDocumentation.documentations.each(&:write)
4339
end
4440
end

lib/rspec_api_documentation/dsl.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,4 @@ def self.resource(*args, &block)
2424
RSpec.configuration.include RspecApiDocumentation::DSL::Resource, :api_doc_dsl => :resource
2525
RSpec.configuration.include RspecApiDocumentation::DSL::Endpoint, :api_doc_dsl => :endpoint
2626
RSpec.configuration.include RspecApiDocumentation::DSL::Callback, :api_doc_dsl => :callback
27-
RSpec.configuration.backtrace_exclusion_patterns << %r{lib/rspec_api_documentation/dsl\.rb}
27+
RSpec.configuration.backtrace_exclusion_patterns << %r{lib/rspec_api_documentation/dsl/}

lib/rspec_api_documentation/dsl/callback.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def trigger_callback(&block)
1616
end
1717

1818
def destination
19-
@destination ||= RspecApiDocumentation::TestServer.new(self)
19+
@destination ||= RspecApiDocumentation::TestServer.new(RSpec.current_example)
2020
end
2121

2222
def callback_url

lib/rspec_api_documentation/dsl/endpoint.rb

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,7 @@ module Endpoint
1212

1313
module ClassMethods
1414
def example_request(description, params = {}, &block)
15-
file_path = caller.first[0, caller.first =~ /:/]
16-
17-
location = caller.first[0, caller.first =~ /(:in|$)/]
18-
location = relative_path(location)
19-
20-
example description, :location => location, :file_path => file_path do
15+
example description, :caller => block.send(:caller) do
2116
do_request(params)
2217
instance_eval &block if block_given?
2318
end
@@ -106,6 +101,10 @@ def explanation(text)
106101
example.metadata[:explanation] = text
107102
end
108103

104+
def example
105+
RSpec.current_example
106+
end
107+
109108
private
110109

111110
def rspec_api_documentation_client

lib/rspec_api_documentation/test_server.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
module RspecApiDocumentation
2-
class TestServer < Struct.new(:context)
2+
class TestServer < Struct.new(:example)
33
include Headers
44

5-
delegate :example, :to => :context
65
delegate :metadata, :to => :example
76

87
attr_reader :request_method, :request_headers, :request_body

rspec_api_documentation.gemspec

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ $:.unshift lib unless $:.include?(lib)
33

44
Gem::Specification.new do |s|
55
s.name = "rspec_api_documentation"
6-
s.version = "3.1.0"
6+
s.version = "4.0.0.pre"
77
s.platform = Gem::Platform::RUBY
88
s.authors = ["Chris Cahoon", "Sam Goldman", "Eric Oestrich"]
99
s.email = ["chris@smartlogicsolutions.com", "sam@smartlogicsolutions.com", "eric@smartlogicsolutions.com"]
@@ -14,7 +14,7 @@ Gem::Specification.new do |s|
1414

1515
s.required_rubygems_version = ">= 1.3.6"
1616

17-
s.add_runtime_dependency "rspec", "~> 2.14.0", ">= 2.14.0"
17+
s.add_runtime_dependency "rspec", "~> 3.0.0"
1818
s.add_runtime_dependency "activesupport", ">= 3.0.0"
1919
s.add_runtime_dependency "i18n", ">= 0.1.0"
2020
s.add_runtime_dependency "mustache", ">= 0.99.4"
@@ -28,6 +28,7 @@ Gem::Specification.new do |s|
2828
s.add_development_dependency "rack-test", ">= 0.6.2"
2929
s.add_development_dependency "rack-oauth2", ">= 0.14.4"
3030
s.add_development_dependency "webmock", ">= 1.7.0"
31+
s.add_development_dependency "rspec-its"
3132

3233
s.files = Dir.glob("lib/**/*") + Dir.glob("templates/**/*")
3334
s.require_path = 'lib'

spec/api_documentation_spec.rb

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@
1515
test_file = configuration.docs_dir.join("test")
1616
FileUtils.mkdir_p configuration.docs_dir
1717
FileUtils.touch test_file
18-
FileUtils.stub(:cp_r)
18+
allow(FileUtils).to receive(:cp_r)
1919
subject.clear_docs
2020

21-
File.directory?(configuration.docs_dir).should be_true
22-
File.exists?(test_file).should be_false
21+
expect(File.directory?(configuration.docs_dir)).to be_truthy
22+
expect(File.exists?(test_file)).to be_falsey
2323
end
2424
end
2525

@@ -30,29 +30,29 @@
3030
let!(:wrapped_example) { RspecApiDocumentation::Example.new(example, configuration) }
3131

3232
before do
33-
RspecApiDocumentation::Example.stub(:new).and_return(wrapped_example)
33+
allow(RspecApiDocumentation::Example).to receive(:new).and_return(wrapped_example)
3434
end
3535

3636
it "should create a new wrapped example" do
37-
RspecApiDocumentation::Example.should_receive(:new).with(example, configuration).and_return(wrapped_example)
37+
expect(RspecApiDocumentation::Example).to receive(:new).with(example, configuration).and_return(wrapped_example)
3838
documentation.document_example(example)
3939
end
4040

4141
context "when the given example should be documented" do
42-
before { wrapped_example.stub(:should_document?).and_return(true) }
42+
before { allow(wrapped_example).to receive(:should_document?).and_return(true) }
4343

4444
it "should add the wrapped example to the index" do
4545
documentation.document_example(example)
46-
documentation.index.examples.should eq([wrapped_example])
46+
expect(documentation.index.examples).to eq([wrapped_example])
4747
end
4848
end
4949

5050
context "when the given example should not be documented" do
51-
before { wrapped_example.stub(:should_document?).and_return(false) }
51+
before { allow(wrapped_example).to receive(:should_document?).and_return(false) }
5252

5353
it "should not add the wrapped example to the index" do
5454
documentation.document_example(example)
55-
documentation.index.examples.should be_empty
55+
expect(documentation.index.examples).to be_empty
5656
end
5757
end
5858
end
@@ -68,8 +68,9 @@ class RspecApiDocumentation::Writers::TextileWriter; end
6868
end
6969

7070
it "should return the classes from format" do
71-
subject.writers.should == [RspecApiDocumentation::Writers::HtmlWriter, RspecApiDocumentation::Writers::JsonWriter,
72-
RspecApiDocumentation::Writers::TextileWriter]
71+
expect(subject.writers).to eq([RspecApiDocumentation::Writers::HtmlWriter,
72+
RspecApiDocumentation::Writers::JsonWriter,
73+
RspecApiDocumentation::Writers::TextileWriter])
7374
end
7475
end
7576

@@ -79,7 +80,7 @@ class RspecApiDocumentation::Writers::TextileWriter; end
7980
end
8081

8182
it "should return the classes from format" do
82-
subject.writers.should == [RspecApiDocumentation::Writers::HtmlWriter]
83+
expect(subject.writers).to eq([RspecApiDocumentation::Writers::HtmlWriter])
8384
end
8485
end
8586
end
@@ -90,13 +91,13 @@ class RspecApiDocumentation::Writers::TextileWriter; end
9091
let(:textile_writer) { double(:textile_writer) }
9192

9293
before do
93-
subject.stub(:writers => [html_writer, json_writer, textile_writer])
94+
allow(subject).to receive(:writers).and_return([html_writer, json_writer, textile_writer])
9495
end
9596

9697
it "should write the docs in each format" do
97-
html_writer.should_receive(:write).with(subject.index, configuration)
98-
json_writer.should_receive(:write).with(subject.index, configuration)
99-
textile_writer.should_receive(:write).with(subject.index, configuration)
98+
expect(html_writer).to receive(:write).with(subject.index, configuration)
99+
expect(json_writer).to receive(:write).with(subject.index, configuration)
100+
expect(textile_writer).to receive(:write).with(subject.index, configuration)
100101
subject.write
101102
end
102103
end

0 commit comments

Comments
 (0)