Permalink
Browse files

Added a doc section on writing tests.

  • Loading branch information...
dblock committed Aug 13, 2011
1 parent 316d863 commit 99bf4b44c511541c0e10f4506bf34ae9abcccd75
Showing with 24 additions and 0 deletions.
  1. +24 −0 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`.

This comment has been minimized.

Show comment
Hide comment
@emj365

emj365 Jun 17, 2015

Only success when add it into rails_helper.rb

@emj365

emj365 Jun 17, 2015

Only success when add it into rails_helper.rb

This comment has been minimized.

Show comment
Hide comment
@emj365

emj365 Jun 17, 2015

Actually, it doesn't work for me.

@emj365

emj365 Jun 17, 2015

Actually, it doesn't work for me.

+
+ 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.

5 comments on commit 99bf4b4

@joshnabbott

This comment has been minimized.

Show comment
Hide comment
@joshnabbott

joshnabbott Sep 26, 2011

How would this work using Cucumber??

How would this work using Cucumber??

@multiplier

This comment has been minimized.

Show comment
Hide comment
@multiplier

multiplier Jan 19, 2012

How about POST to grape

How about POST to grape

@manageyp

This comment has been minimized.

Show comment
Hide comment
@manageyp

manageyp Jul 3, 2013

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}'

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}'

@dathi

This comment has been minimized.

Show comment
Hide comment
@dathi

dathi Sep 23, 2015

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

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

@Faisal-nfl

This comment has been minimized.

Show comment
Hide comment
@Faisal-nfl

Faisal-nfl May 18, 2018

What the word "Twitter" in
describe Twitter::API do points to?

Faisal-nfl replied May 18, 2018

What the word "Twitter" in
describe Twitter::API do points to?

Please sign in to comment.