Skip to content

Instantly share code, notes, and snippets.

@gmodarelli
Last active August 29, 2015 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gmodarelli/11177037 to your computer and use it in GitHub Desktop.
Save gmodarelli/11177037 to your computer and use it in GitHub Desktop.
Testing API with RSpec
RSpec::Matchers.define :be_a_valid_api_response do
match do |json_response|
json_response['version'] == '1.0' &&
json_response['response']['status'] == 0 &&
json_response['response']['reason'] == 'Request successfully processed'
end
end
it "returns a 200 HTTP status" do
expect(response).to be_success
end
it "returns a valid JSON response" do
expect(json).to be_a_valid_api_response
end
{
"version": "1.0",
"response": {
"status": 0,
"reason": "Request successfully processed"
},
"continents": [{
"id": "528ce3d151f3fc3ef3000002",
"name": "Europe",
"image": "http://domain.tdl/path/to/image.png"
}, {
"id": "528ce3d151f3fc3ef3000012",
"name": "Asia",
"image": "http://domain.tdl/path/to/image.png"
}]
}
require 'spec_helper'
describe 'Continents API' do
describe 'continents.json' do
before do
%w[Europe Asia America Africa Oceania].each do |name|
FactoryGirl.create :continent, name: name
end
get '/api/v1/roaming/continents.json'
end
it "returns a 200 HTTP status" do
expect(response).to be_success
end
it "returns a valid JSON response" do
expect(json).to be_a_valid_api_response
end
it "returns the right number of continents" do
expect(json['continents'].lenght).to eq(5)
end
it "returns the continents with the right format" do
json['continents'].each do |continent|
expect(continent).to match_the_format(
"id", "name", "image")
end
end
end
end
require 'spec_helper'
describe 'Continents API' do
describe 'continents.json' do
it "returns a 200 HTTP status" do
get '/api/v1/roaming/continents.json'
expect(response).to be_success
end
end
end
...
it "returns a valid JSON response" do
get '/api/v1/roaming/continents.json'
json = JSON.parse(response.body)
expect(json['version']).to eq("1.0")
expect(json['response']['status']).to eq(0)
expect(json['response']['reason']).to eq("Request successfully processed")
end
...
...
before do
get '/api/v1/roaming/continents.json'
end
...
require 'spec_helper'
describe 'Continents API' do
describe 'continents.json' do
before do
get '/api/v1/roaming/continents.json'
end
it "returns a 200 HTTP status" do
expect(response).to be_success
end
it "returns a valid JSON response" do
expect(json).to be_a_valid_api_response
end
end
end
...
before do
%w[Europe Asia America Africa Oceania].each do |name|
FactoryGirl.create :continent, name: name
end
get '/api/v1/roaming/continents.json'
end
...
it "returns the right number of continents" do
expect(json['continents'].lenght).to eq(5)
end
...
...
it "returns the continents with the right format" do
json['continents'].each do |continent|
expect(continent.keys).to eq(["id", "name", "image"])
end
end
...
require 'spec_helper'
require 'set'
...
it "returns the continents with the right format" do
json['continents'].each do |continent|
expect(continent.keys.to_set).to eq(
["id", "name", "image"].to_set
)
end
end
...
module Request
module JsonHelpers
def json
@json ||= JSON.parse(response.body)
end
end
end
RSpec.configure do |config|
config.include Requests::JsonHelpers, type: :request
end
RSpec::Matchers.define :match_the_format do |*expected_format|
require 'set'
match do |json_resource|
json_resource.keys.to_set == expected_format.to_set
end
end
shared_example "a successful api request" do
it "returns a 200 HTTP status" do
expect(response).to be_success
end
it "returns a valid JSON response" do
expect(json).to be_a_valid_api_response
end
end
require 'spec_helper'
describe 'Continents API' do
describe 'continents.json' do
...
it_behaves_like "a successful api request"
...
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment