SlideShare a Scribd company logo
TESTING SERVICES
effectively
Alberto Leal @albertoleal
http://albertoleal.me
What is a service?
External

Service
Internal
Service
ad$
A
http://api.example.com/cars
Request Response
Connection: keep-alive	
Content-Length: 1466	
Content-Type: application/json; 	
Date: Sat, 02 Aug 2014 19:42:38 GMT	
ETag: W/"5ba-70348105"
{	
"cars": [	
{	
"created": "2014-07-25T19:27:56.919Z",	
"id": "53d2afbc7165355f0eaf79da",	
"modified": "2014-07-25T19:27:56.919Z",	
"name": "Ferrari"	
},	
{	
"created": "2014-07-25T19:27:56.874Z",	
"id": "53d2afbc7165355f0eaf79d9",	
"modified": "2014-07-25T19:27:56.874Z",	
"name": "Porshe"	
}	
	 ]	
}
Request Response
http://api.example.com/cars
Connection: keep-alive	
Content-Length: 1466	
Content-Type: application/json; 	
Date: Sat, 02 Aug 2014 19:42:38 GMT	
ETag: W/"5ba-70348105"
{	
"cars": [	
{	
"created": "2014-07-25T19:27:56.919Z",	
"id": "53d2afbc7165355f0eaf79da",	
"modified": "2014-07-25T19:27:56.919Z",	
"name": "Ferrari"	
},	
{	
"created": "2014-07-25T19:27:56.874Z",	
"id": "53d2afbc7165355f0eaf79d9",	
"modified": "2014-07-25T19:27:56.874Z",	
"name": "Porshe"	
}	
	 ]	
}
headers
Connection: keep-alive	
Content-Length: 1466	
Content-Type: application/json; 	
Date: Sat, 02 Aug 2014 19:42:38 GMT	
ETag: W/"5ba-70348105"
{	
"cars": [	
{	
"created": "2014-07-25T19:27:56.919Z",	
"id": "53d2afbc7165355f0eaf79da",	
"modified": "2014-07-25T19:27:56.919Z",	
"name": "Ferrari"	
},	
{	
"created": "2014-07-25T19:27:56.874Z",	
"id": "53d2afbc7165355f0eaf79d9",	
"modified": "2014-07-25T19:27:56.874Z",	
"name": "Porshe"	
}	
	 ]	
}
body
Failures:	
!
1) Car Service should retrieve all cars	
Failure/Error: response = RestClient.get ‘api.example.com/cars'	
SocketError:	
getaddrinfo: nodename nor servname provided, or not known	
# ./test_spec.rb:12:in `block (2 levels) in <top (required)>'	
!
Finished in 0.02436 seconds (files took 0.81924 seconds to load)	
1 example, 1 failure	
!
Failed examples:	
!
rspec ./test_spec.rb:11 # External Service should retrieve all cars
gem 'webmock'
https://github.com/bblimke/webmock
require 'webmock/rspec'	
require 'rest_client'	
!
describe 'Car Service’ do	
it 'should retrieve all cars' do	
response = RestClient.get ‘api.example.com/cars'	
expect(response).to be_an_instance_of(String)	
end	
end
------------------------------	
FAIL: 1 PASS: 0 PENDING: 0	
------------------------------	
Finished in 0.04571 seconds	
	
[Car Service]	
- should retrieve all cars	
Real HTTP connections are disabled. Unregistered request: GET http://
api.example.com/cars with headers {'Accept'=>'*/*; q=0.5, application/
xml', 'Accept-Encoding'=>'gzip, deflate', 'User-Agent'=>'Ruby'}You can
stub this request with the following snippet:stub_request(:get, “http://
api.example.com/cars”). with(:headers => {'Accept'=>'*/*; q=0.5,
application/xml', 'Accept-Encoding'=>'gzip, deflate', 'User-
Agent'=>'Ruby'}). to_return(:status => 200, :body => "", :headers => {})
require 'webmock/rspec'	
require 'rest_client'	
!
WebMock.allow_net_connect!	
!
describe 'Car Service' do	
it 'should retrieve all cars' do	
response = RestClient.get ‘api.example.com/cars'	
expect(response).to be_an_instance_of(String)	
end	
end
require 'webmock/rspec'	
require 'rest_client'	
!
# WebMock.allow_net_connect!	
!
describe 'Car Service' do	
before :all do	
stub_request(:get, ‘api.example.com/cars')	
	 .to_return(status: 200, headers: {}, body: 'This is a mock.')	
end	
!
it 'should retrieve all cars' do	
response = RestClient.get ‘http://api.example.com/cars'	
expect(response).to eq(‘This is a mock.')	
end	
end
gem 'vcr'gem 'vcr'
https://github.com/vcr/vcr
ENV["RAILS_ENV"] ||= 'test'	
require File.expand_path("../../config/environment", __FILE__)	
require 'rspec/rails'	
require 'rspec/autorun'	
!
RSpec.configure do |config|	
config.mock_with :rspec	
config.infer_base_class_for_anonymous_controllers = false	
config.order = "random"	
!
#Configuring VCR gem	
config.around(:each, :vcr) do |example|	
opts = example.metadata.slice(:record, :match_requests_on).except(:example_group)	
VCR.use_cassette(example.metadata[:cassette_name], opts) { example.call }	
end	
end
describe Car do	
...	
	
	 describe '.all' do	
	it 'should retrieve all available cars', :vcr, cassette_name: ‘cars/all’ do	
		 cars = Car.all	
	 	 expect(cars).to be_an_instance_of Array	
	 	 	 expect(cars).to have(2).items	
	end	
	end	
!
...	
end
gem 'puffing-billy'
https://github.com/oesmith/puffing-billy
require ‘billy/rspec'	
!
Capybara.javascript_driver = :selenium_billy
proxy.stub(‘http://api.example.com/cars').and_return(json: {cars: […]})
Integration

Tests
Contract
Tests
Consumer-Driven Contracts:
A Service Evolution Pattern
http://martinfowler.com/articles/consumerDrivenContracts.html
Contracts can couple
service providers
and
consumers
gem 'pact'
https://github.com/realestate-com-au/pact
class MyServiceProviderClient	
include HTTParty	
base_uri 'http://my-service'	
!
def get_something	
	 	 name = JSON.parse(self.class.get("/something").body)['name']	
Something.new(name)	
end	
end
Client
require 'pact/consumer/rspec'	
!
Pact.service_consumer "My Service Consumer" do	
has_pact_with "My Service Provider" do	
mock_service :my_service_provider do	
port 1234	
end	
end	
end
Mock Server
describe MyServiceProviderClient, pact: true do	
!
before do	
# Configure your client to point to the stub service on localhost using the port you have specified	
MyServiceProviderClient.base_uri 'localhost:1234'	
end	
!
subject { MyServiceProviderClient.new }	
!
describe "get_something" do	
!
before do	
my_service_provider.given("something exists").	
upon_receiving("a request for something").with(method: :get, path: '/something').	
will_respond_with(	
status: 200,	
headers: {'Content-Type' => 'application/json'},	
body: {name: 'A small something'} )	
end	
!
it "returns a Something" do	
expect(subject.get_something).to eq(Something.new('A small something'))	
end	
!
end	
!
end
thanks!
http://albertoleal.me
https://www.flickr.com/photos/robsmits/5452184206
https://www.flickr.com/photos/hotcherry/521006473
Photos:
https://www.flickr.com/photos/vern/5379218273/sizes/l

More Related Content

Similar to Testing Services Effectively

RESTful API を Chalice で紐解く 〜 Python Serverless Microframework for AWS 〜
RESTful API を Chalice で紐解く 〜 Python Serverless Microframework for AWS 〜RESTful API を Chalice で紐解く 〜 Python Serverless Microframework for AWS 〜
RESTful API を Chalice で紐解く 〜 Python Serverless Microframework for AWS 〜
崇之 清水
 
Psgi Plack Sfpm
Psgi Plack SfpmPsgi Plack Sfpm
Psgi Plack Sfpm
som_nangia
 
Psgi Plack Sfpm
Psgi Plack SfpmPsgi Plack Sfpm
Psgi Plack Sfpm
wilburlo
 

Similar to Testing Services Effectively (20)

Summit2011 satellites-robinf-20110605
Summit2011 satellites-robinf-20110605Summit2011 satellites-robinf-20110605
Summit2011 satellites-robinf-20110605
 
Mасштабирование микросервисов на Go, Matt Heath (Hailo)
Mасштабирование микросервисов на Go, Matt Heath (Hailo)Mасштабирование микросервисов на Go, Matt Heath (Hailo)
Mасштабирование микросервисов на Go, Matt Heath (Hailo)
 
EdgeWorkers: Enabling Autonomous, Developer Friendly Programming at the Edge
EdgeWorkers: Enabling Autonomous, Developer Friendly Programming at the EdgeEdgeWorkers: Enabling Autonomous, Developer Friendly Programming at the Edge
EdgeWorkers: Enabling Autonomous, Developer Friendly Programming at the Edge
 
Tadhack madrid June 2014: Joris Swinnen and WebRTC Nederland "Invite my colle...
Tadhack madrid June 2014: Joris Swinnen and WebRTC Nederland "Invite my colle...Tadhack madrid June 2014: Joris Swinnen and WebRTC Nederland "Invite my colle...
Tadhack madrid June 2014: Joris Swinnen and WebRTC Nederland "Invite my colle...
 
RESTful API を Chalice で紐解く 〜 Python Serverless Microframework for AWS 〜
RESTful API を Chalice で紐解く 〜 Python Serverless Microframework for AWS 〜RESTful API を Chalice で紐解く 〜 Python Serverless Microframework for AWS 〜
RESTful API を Chalice で紐解く 〜 Python Serverless Microframework for AWS 〜
 
Session 4
Session 4Session 4
Session 4
 
Rack Middleware
Rack MiddlewareRack Middleware
Rack Middleware
 
Building Single Page Application (SPA) with Symfony2 and AngularJS
Building Single Page Application (SPA) with Symfony2 and AngularJSBuilding Single Page Application (SPA) with Symfony2 and AngularJS
Building Single Page Application (SPA) with Symfony2 and AngularJS
 
Protocol-Oriented Programming in Swift
Protocol-Oriented Programming in SwiftProtocol-Oriented Programming in Swift
Protocol-Oriented Programming in Swift
 
Build a boat with node.js and spark.io
Build a boat with node.js and spark.ioBuild a boat with node.js and spark.io
Build a boat with node.js and spark.io
 
How React Native, Appium and me made each other shine @ContinuousDeliveryAmst...
How React Native, Appium and me made each other shine @ContinuousDeliveryAmst...How React Native, Appium and me made each other shine @ContinuousDeliveryAmst...
How React Native, Appium and me made each other shine @ContinuousDeliveryAmst...
 
JSON and the APInauts
JSON and the APInautsJSON and the APInauts
JSON and the APInauts
 
Pyrax talk
Pyrax talkPyrax talk
Pyrax talk
 
Using Sinatra to Build REST APIs in Ruby
Using Sinatra to Build REST APIs in RubyUsing Sinatra to Build REST APIs in Ruby
Using Sinatra to Build REST APIs in Ruby
 
Sst hackathon express
Sst hackathon expressSst hackathon express
Sst hackathon express
 
Re:inventing EC2 Instance Launches with Launch Templates - SRV335 - Chicago A...
Re:inventing EC2 Instance Launches with Launch Templates - SRV335 - Chicago A...Re:inventing EC2 Instance Launches with Launch Templates - SRV335 - Chicago A...
Re:inventing EC2 Instance Launches with Launch Templates - SRV335 - Chicago A...
 
Psgi Plack Sfpm
Psgi Plack SfpmPsgi Plack Sfpm
Psgi Plack Sfpm
 
Psgi Plack Sfpm
Psgi Plack SfpmPsgi Plack Sfpm
Psgi Plack Sfpm
 
Pyramid Lighter/Faster/Better web apps
Pyramid Lighter/Faster/Better web appsPyramid Lighter/Faster/Better web apps
Pyramid Lighter/Faster/Better web apps
 
PSGI/Plack OSDC.TW
PSGI/Plack OSDC.TWPSGI/Plack OSDC.TW
PSGI/Plack OSDC.TW
 

More from Alberto Leal (6)

Designing the APIs for an internal set of services
Designing the APIs for an internal set of servicesDesigning the APIs for an internal set of services
Designing the APIs for an internal set of services
 
Contribuindo com a comunidade Open Source
Contribuindo com a comunidade Open SourceContribuindo com a comunidade Open Source
Contribuindo com a comunidade Open Source
 
O que é dojo
O que é dojoO que é dojo
O que é dojo
 
Caipira Ágil - Afinal, testes atrasam o desenvolvimento?
Caipira Ágil - Afinal, testes atrasam o desenvolvimento?Caipira Ágil - Afinal, testes atrasam o desenvolvimento?
Caipira Ágil - Afinal, testes atrasam o desenvolvimento?
 
Git - O Rebase Pode Te Assustar
Git - O Rebase Pode Te AssustarGit - O Rebase Pode Te Assustar
Git - O Rebase Pode Te Assustar
 
Prazer,Ruby On Rails
Prazer,Ruby On RailsPrazer,Ruby On Rails
Prazer,Ruby On Rails
 

Recently uploaded

Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
Safe Software
 

Recently uploaded (20)

Free and Effective: Making Flows Publicly Accessible, Yumi Ibrahimzade
Free and Effective: Making Flows Publicly Accessible, Yumi IbrahimzadeFree and Effective: Making Flows Publicly Accessible, Yumi Ibrahimzade
Free and Effective: Making Flows Publicly Accessible, Yumi Ibrahimzade
 
Agentic RAG What it is its types applications and implementation.pdf
Agentic RAG What it is its types applications and implementation.pdfAgentic RAG What it is its types applications and implementation.pdf
Agentic RAG What it is its types applications and implementation.pdf
 
PLAI - Acceleration Program for Generative A.I. Startups
PLAI - Acceleration Program for Generative A.I. StartupsPLAI - Acceleration Program for Generative A.I. Startups
PLAI - Acceleration Program for Generative A.I. Startups
 
Unpacking Value Delivery - Agile Oxford Meetup - May 2024.pptx
Unpacking Value Delivery - Agile Oxford Meetup - May 2024.pptxUnpacking Value Delivery - Agile Oxford Meetup - May 2024.pptx
Unpacking Value Delivery - Agile Oxford Meetup - May 2024.pptx
 
Exploring UiPath Orchestrator API: updates and limits in 2024 🚀
Exploring UiPath Orchestrator API: updates and limits in 2024 🚀Exploring UiPath Orchestrator API: updates and limits in 2024 🚀
Exploring UiPath Orchestrator API: updates and limits in 2024 🚀
 
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
 
What's New in Teams Calling, Meetings and Devices April 2024
What's New in Teams Calling, Meetings and Devices April 2024What's New in Teams Calling, Meetings and Devices April 2024
What's New in Teams Calling, Meetings and Devices April 2024
 
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered QualitySoftware Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
 
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
 
Powerful Start- the Key to Project Success, Barbara Laskowska
Powerful Start- the Key to Project Success, Barbara LaskowskaPowerful Start- the Key to Project Success, Barbara Laskowska
Powerful Start- the Key to Project Success, Barbara Laskowska
 
Introduction to Open Source RAG and RAG Evaluation
Introduction to Open Source RAG and RAG EvaluationIntroduction to Open Source RAG and RAG Evaluation
Introduction to Open Source RAG and RAG Evaluation
 
10 Differences between Sales Cloud and CPQ, Blanka Doktorová
10 Differences between Sales Cloud and CPQ, Blanka Doktorová10 Differences between Sales Cloud and CPQ, Blanka Doktorová
10 Differences between Sales Cloud and CPQ, Blanka Doktorová
 
JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and Grafana
 
UiPath Test Automation using UiPath Test Suite series, part 1
UiPath Test Automation using UiPath Test Suite series, part 1UiPath Test Automation using UiPath Test Suite series, part 1
UiPath Test Automation using UiPath Test Suite series, part 1
 
Knowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and backKnowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and back
 
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...
 

Testing Services Effectively