Skip to content
Browse files

Added a doc section on writing tests.

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

Only success when add it into rails_helper.rb

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.

4 comments on commit 99bf4b4


How would this work using Cucumber??


How about POST to grape


You can get some examples from the original Grape gem's source files.

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


Thanks, it works for me.
Ruby 2.2.2
Rails 4.2.3
Grape 0.13.0

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