Skip to content

Commit 9b63d92

Browse files
committed
Documented testing with RACK.
1 parent d0d599f commit 9b63d92

File tree

1 file changed

+42
-9
lines changed

1 file changed

+42
-9
lines changed

README.markdown

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -282,20 +282,42 @@ end
282282

283283
## Writing Tests
284284

285-
You can test a Grape API with RSpec. Tests make HTTP requests, therefore they
286-
must go into the `spec/request` group. You may want your API code to go into
287-
`app/api` - you can match that layout under `spec` by adding the following in
288-
`spec/spec_helper.rb`.
285+
You can test a Grape API with RSpec by making HTTP requests and examining the response.
286+
287+
### Writing Tests with Rack
288+
289+
Use `rack-test` and define your API as `app`.
289290

290291
```ruby
291-
RSpec.configure do |config|
292-
config.include RSpec::Rails::RequestExampleGroup, :type => :request, :example_group => {
293-
:file_path => /spec\/api/
294-
}
292+
require 'spec_helper'
293+
294+
describe Twitter::API do
295+
include Rack::Test::Methods
296+
297+
def app
298+
Twitter::API
299+
end
300+
301+
describe Twitter::API do
302+
describe "GET /api/v1/statuses" do
303+
it "returns an empty array of statuses" do
304+
get "/api/v1/statuses"
305+
last_response.status.should == 200
306+
JSON.parse(response.body).should == []
307+
end
308+
end
309+
describe "GET /api/v1/statuses/:id" do
310+
it "returns a status by id" do
311+
status = Status.create!
312+
get "/api/v1/statuses/#{status.id}"
313+
last_resonse.body.should == status.to_json
314+
end
315+
end
316+
end
295317
end
296318
```
297319

298-
A simple RSpec API test makes a `get` request and parses the response.
320+
### Writing Tests with Rails
299321

300322
```ruby
301323
require 'spec_helper'
@@ -318,6 +340,17 @@ describe Twitter::API do
318340
end
319341
```
320342

343+
In Rails, HTTP request tests would go into the `spec/request` group. You may want your API code to go into
344+
`app/api` - you can match that layout under `spec` by adding the following in `spec/spec_helper.rb`.
345+
346+
```ruby
347+
RSpec.configure do |config|
348+
config.include RSpec::Rails::RequestExampleGroup, :type => :request, :example_group => {
349+
:file_path => /spec\/api/
350+
}
351+
end
352+
```
353+
321354
## Describing and Inspecting an API
322355

323356
Grape lets you add a description to an API along with any other optional

0 commit comments

Comments
 (0)