Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added a doc section on writing tests.

commit 99bf4b44c511541c0e10f4506bf34ae9abcccd75 1 parent 316d863
@dblock authored
Showing with 24 additions and 0 deletions.
  1. +24 −0 README.markdown
View
24 README.markdown
@@ -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`.
@emj365
emj365 added a note

Only success when add it into rails_helper.rb

@emj365
emj365 added a note

Actually, it doesn't work for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+
+ 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

@joshnabbott

How would this work using Cucumber??

@multiplier

How about POST to grape

@manageyp

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.