Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added a doc section on writing tests.

commit 99bf4b44c511541c0e10f4506bf34ae9abcccd75 1 parent 316d863
Daniel Doubrovkine (dB.) @dblockdotorg authored
Showing with 24 additions and 0 deletions.
  1. +24 −0 README.markdown
24 README.markdown
View
@@ -83,6 +83,30 @@ you simply use the `rescue_from` method inside your API declaration:
rescue_from ArgumentError, NotImplementedError # :all for all errors
end
+## Writing Tests
+
+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`.
+
+ RSpec.configure do |config|
+ config.include RSpec::Rails::RequestExampleGroup, :type => :request, :example_group => {
+ :file_path => /spec\/api/
+ }
+ end
+
+A simple RSpec API test makes a `get` request and parses the response.
+
+ require 'spec_helper'
+
+ describe Twitter::API do
+ describe "GET /api/v1/statuses" do
+ it "returns an empty array of statuses" do
+ get "/api/v1/statuses"
+ response.status.should == 200
+ JSON.parse(response.body).should == []
+ end
+ end
+ end
+
## Note on Patches/Pull Requests
* Fork the project.

3 comments on commit 99bf4b4

Josh N. Abbott

How would this work using Cucumber??

John Kamuchau

How about POST to grape

Henry

You can get some examples from the original Grape gem's source files.
Link: https://github.com/intridea/grape/blob/master/spec/grape/api_spec.rb

Such as:
post "/data", '{"x":42}', { 'CONTENT_TYPE' => 'application/json' }
last_response.status.should == 201
last_response.body.should == '{"x":42}'

Please sign in to comment.
Something went wrong with that request. Please try again.