Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Added a doc section on writing tests.

commit 99bf4b44c511541c0e10f4506bf34ae9abcccd75 1 parent 316d863
Daniel Doubrovkine (dB.) @dblockdotorg authored August 13, 2011

Showing 1 changed file with 24 additions and 0 deletions. Show diff stats Hide diff stats

  1. 24  README.markdown
24  README.markdown
Source Rendered
@@ -83,6 +83,30 @@ you simply use the `rescue_from` method inside your API declaration:
83 83
       rescue_from ArgumentError, NotImplementedError # :all for all errors
84 84
     end
85 85
 
  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
+
86 110
 ## Note on Patches/Pull Requests
87 111
  
88 112
 * Fork the project.

3 notes 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.