Skip to content

Commit 76068c4

Browse files
author
Michael Bleigh
committed
Merge pull request ruby-grape#56 from dblock/readme-writing-tests
Added a doc section on writing tests.
2 parents 316d863 + 99bf4b4 commit 76068c4

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

README.markdown

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,30 @@ you simply use the `rescue_from` method inside your API declaration:
8383
rescue_from ArgumentError, NotImplementedError # :all for all errors
8484
end
8585

86+
## Writing Tests
87+
88+
You can test a Grape API with RSpec. Tests make HTTP requests, therefore they must go into the `spec/request` group. You may want your API code to go into `app/api` - you can match that layout under `spec` by adding the following in `spec/spec_helper.rb`.
89+
90+
RSpec.configure do |config|
91+
config.include RSpec::Rails::RequestExampleGroup, :type => :request, :example_group => {
92+
:file_path => /spec\/api/
93+
}
94+
end
95+
96+
A simple RSpec API test makes a `get` request and parses the response.
97+
98+
require 'spec_helper'
99+
100+
describe Twitter::API do
101+
describe "GET /api/v1/statuses" do
102+
it "returns an empty array of statuses" do
103+
get "/api/v1/statuses"
104+
response.status.should == 200
105+
JSON.parse(response.body).should == []
106+
end
107+
end
108+
end
109+
86110
## Note on Patches/Pull Requests
87111

88112
* Fork the project.

0 commit comments

Comments
 (0)