Last active
August 29, 2015 14:00
-
-
Save gmodarelli/11177037 to your computer and use it in GitHub Desktop.
Testing API with RSpec
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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" | |
}] | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
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 | |
... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
before do | |
get '/api/v1/roaming/continents.json' | |
end | |
... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
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 | |
... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
it "returns the continents with the right format" do | |
json['continents'].each do |continent| | |
expect(continent.keys).to eq(["id", "name", "image"]) | |
end | |
end | |
... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment