SlideShare a Scribd company logo
1 of 17
Download to read offline
VVCCRR 
@revathskumar 
VCR 
1 of 17 @revathskumar Sunday 21 September 2014 10:24 PM
AABBOOUUTT 
Rubyist / JavaScripter 
Yeoman Team Member / @keralarb / 
@keralajs 
Works at 
Blog at 
Twitter/Github - 
Google+: 
@whatznear 
blog.revathskumar.com 
@revathskumar 
+RevathSKumar 
VCR 
2 of 17 @revathskumar Sunday 21 September 2014 10:24 PM
WHATZNEAR AARRCCHHIITTEECCTTUURREE 
VCR 
3 of 17 @revathskumar Sunday 21 September 2014 10:24 PM
WWEEBBMMOOCCKK 
Stubbing HTTP requests at low http client lib level 
Support multiple HTTP library 
Setting and verifying expectations on HTTP requests 
Matching requests based on method, URI, headers and body 
VCR 
4 of 17 @revathskumar Sunday 21 September 2014 10:24 PM
SSEETTUUPP 
# Gemfile 
group :test do 
gem 'webmock' 
end 
# test/test_helper.rb 
require 'webmock/test_unit' 
VCR 
5 of 17 @revathskumar Sunday 21 September 2014 10:24 PM
SSTTUUBBBBIINNGG 
stub_request(:post, "www.example.com"). 
with(:body => {:data => {:a => '1', :b => 'five'}}) 
stub_request(:get, "www.example.com"). 
to_return(body: "{:data => {:a => '1', :b => 'five'}}", status: 200) 
VCR 
6 of 17 @revathskumar Sunday 21 September 2014 10:24 PM
SSTTUUBBBBIINNGG FFRROOMM FFIILLEE 
# test/test_helper.rb 
require 'webmock/test_unit' 
class ActiveSupport::TestCase 
def load_fixtures name 
path = File.join(Rails.root, "test", "fixtures", "#{name}.json") 
return nil unless File.exists?(path) 
File.read path 
end 
end 
stub_request(:any, "www.example.com"). 
to_return(:body => load_fixtures('get_products'), :status => 200) 
VCR 
7 of 17 @revathskumar Sunday 21 September 2014 10:24 PM
BUT GGOOTT IINNTTOO TTRROOUUBBLLEESS 
half of my time went to setup and maintain fixture 
thought of setting up factories, but still problem with structure 
of JSON response 
VCR 
8 of 17 @revathskumar Sunday 21 September 2014 10:24 PM
VVCCRR 
Make first request to API and save the response 
replay the saved reponse from second request onwards 
VCR 
9 of 17 @revathskumar Sunday 21 September 2014 10:24 PM
VCR 
10 of 17 @revathskumar Sunday 21 September 2014 10:24 PM
SSEETTUUPP 
# test/test_helper.rb 
require 'vcr' 
require 'webmock/minitest' 
VCR.configure do | c | 
c.cassette_library_dir = 'test/fixtures/vcr_cassettes' 
c.hook_into :webmock 
end 
VCR 
11 of 17 @revathskumar Sunday 21 September 2014 10:24 PM
VCR.use_cassette 'carts/empty', record: :new_episodes do 
get :show, format: :json 
assert_response :success 
assert_template 'show.json' 
assert_equal 0, JSON.parse(@response.body)['count'] 
end 
VCR 
12 of 17 @revathskumar Sunday 21 September 2014 10:24 PM
test 'remove item' do 
VCR.use_cassette 'carts/delete', record: :new_episodes do 
item = {"id"=>"2816", "uri"=>"",..} 
store = {"id"=>"prestige", "name"=>"Prestige Smart Kitchen"} 
post :create, format: :json, item: item, store: store_fixture 
delete :destroy, format: :json, id: 2816, store: 'prestige' 
assert_response :success 
assert_template 'show.json' 
assert_equal 0, JSON.parse(@response.body)['count'] 
end 
end 
VCR 
13 of 17 @revathskumar Sunday 21 September 2014 10:24 PM
VVCCRR :: RREECCOORRDD MMOODDEESS 
:once 
:new_episodes 
:none 
:all 
VCR 
14 of 17 @revathskumar Sunday 21 September 2014 10:24 PM
RREEQQUUEESSTT MMAATTCCHHIINNGG 
VCR.use_cassette 'carts/empty', record: :new_episodes, 
match_requests_on: [:method, :uri, :body] do 
get :show, format: :json 
assert_response :success 
assert_template 'show.json' 
assert_equal 0, JSON.parse(@response.body)['count'] 
end 
:method 
:uri 
:body 
:host 
:path 
:headers 
:query 
VCR 
15 of 17 @revathskumar Sunday 21 September 2014 10:24 PM
class UsersControllerTest < ActionController::TestCase 
def setup 
VCR.insert_cassette 'users', record: :new_episodes 
end 
def teardown 
VCR.eject_cassette 'users' 
end 
... 
end 
VCR 
16 of 17 @revathskumar Sunday 21 September 2014 10:24 PM
VCR 
17 of 17 @revathskumar Sunday 21 September 2014 10:24 PM

More Related Content

What's hot

What's hot (13)

Rails after 5 years
Rails after 5 yearsRails after 5 years
Rails after 5 years
 
Test driven infrastructure
Test driven infrastructureTest driven infrastructure
Test driven infrastructure
 
Ember.js Meetup Brussels 31/10/2013
Ember.js Meetup Brussels 31/10/2013Ember.js Meetup Brussels 31/10/2013
Ember.js Meetup Brussels 31/10/2013
 
Websocket on Rails
Websocket on RailsWebsocket on Rails
Websocket on Rails
 
Capistrano
CapistranoCapistrano
Capistrano
 
Capistrano Overview
Capistrano OverviewCapistrano Overview
Capistrano Overview
 
WebRTC Conference Prototype Implementation
WebRTC Conference Prototype ImplementationWebRTC Conference Prototype Implementation
WebRTC Conference Prototype Implementation
 
Server Core with Morden C
Server Core with Morden CServer Core with Morden C
Server Core with Morden C
 
Vagrant and Configuration Management
Vagrant and Configuration ManagementVagrant and Configuration Management
Vagrant and Configuration Management
 
Scaling Deployment at Etsy
Scaling Deployment at EtsyScaling Deployment at Etsy
Scaling Deployment at Etsy
 
One vagrantfile to rule them all
One vagrantfile to rule them allOne vagrantfile to rule them all
One vagrantfile to rule them all
 
Orchestrated Functional Testing with Puppet-spec and Mspectator - PuppetConf ...
Orchestrated Functional Testing with Puppet-spec and Mspectator - PuppetConf ...Orchestrated Functional Testing with Puppet-spec and Mspectator - PuppetConf ...
Orchestrated Functional Testing with Puppet-spec and Mspectator - PuppetConf ...
 
Intro to MQ
Intro to MQIntro to MQ
Intro to MQ
 

Viewers also liked

1 ph topic 8 resistors and inductors in series
1 ph topic 8 resistors and inductors in series1 ph topic 8 resistors and inductors in series
1 ph topic 8 resistors and inductors in series
mattnlis
 
What type of battery do you need
What type of battery do you needWhat type of battery do you need
What type of battery do you need
dneubert
 
6) safety fire safety & electrical safety(engr. ding)
6) safety   fire  safety & electrical safety(engr. ding)6) safety   fire  safety & electrical safety(engr. ding)
6) safety fire safety & electrical safety(engr. ding)
Mark Terry Miraña
 
Electrical Safety
Electrical SafetyElectrical Safety
Electrical Safety
mrmackenzie
 
New Electrical Safety Present2009
New Electrical Safety Present2009New Electrical Safety Present2009
New Electrical Safety Present2009
Nestor Fraga
 
Microphone types and characteristics essay
Microphone types and characteristics essayMicrophone types and characteristics essay
Microphone types and characteristics essay
JoshGmanMcLean
 

Viewers also liked (20)

cellphone repair chennai
cellphone repair chennaicellphone repair chennai
cellphone repair chennai
 
1 ph topic 8 resistors and inductors in series
1 ph topic 8 resistors and inductors in series1 ph topic 8 resistors and inductors in series
1 ph topic 8 resistors and inductors in series
 
Clean and Green Energy Storage
Clean and Green Energy StorageClean and Green Energy Storage
Clean and Green Energy Storage
 
Inductors and Smps
Inductors and SmpsInductors and Smps
Inductors and Smps
 
What type of battery do you need
What type of battery do you needWhat type of battery do you need
What type of battery do you need
 
All About Workplace Electrical Safety
All About Workplace Electrical SafetyAll About Workplace Electrical Safety
All About Workplace Electrical Safety
 
Transistor history and types
Transistor history and typesTransistor history and types
Transistor history and types
 
Semiconductor
SemiconductorSemiconductor
Semiconductor
 
‫What are conductors_and_insulators - نسخة
‫What are conductors_and_insulators - نسخة‫What are conductors_and_insulators - نسخة
‫What are conductors_and_insulators - نسخة
 
Electrical safety
Electrical safetyElectrical safety
Electrical safety
 
Occupational Health And Safety - Electrical Equipment In The Workplace
Occupational Health And Safety - Electrical Equipment In The WorkplaceOccupational Health And Safety - Electrical Equipment In The Workplace
Occupational Health And Safety - Electrical Equipment In The Workplace
 
Inductors, how they work
Inductors, how they workInductors, how they work
Inductors, how they work
 
Battery
BatteryBattery
Battery
 
The squirrel-cage induction motor
The squirrel-cage induction motorThe squirrel-cage induction motor
The squirrel-cage induction motor
 
Electrical Power Systems Induction motor
Electrical Power Systems Induction motorElectrical Power Systems Induction motor
Electrical Power Systems Induction motor
 
6) safety fire safety & electrical safety(engr. ding)
6) safety   fire  safety & electrical safety(engr. ding)6) safety   fire  safety & electrical safety(engr. ding)
6) safety fire safety & electrical safety(engr. ding)
 
Transmission system
Transmission systemTransmission system
Transmission system
 
Electrical Safety
Electrical SafetyElectrical Safety
Electrical Safety
 
New Electrical Safety Present2009
New Electrical Safety Present2009New Electrical Safety Present2009
New Electrical Safety Present2009
 
Microphone types and characteristics essay
Microphone types and characteristics essayMicrophone types and characteristics essay
Microphone types and characteristics essay
 

More from Revath S Kumar (11)

Rack
RackRack
Rack
 
Meetups
MeetupsMeetups
Meetups
 
gulp
gulpgulp
gulp
 
Unit testing with mocha
Unit testing with mochaUnit testing with mocha
Unit testing with mocha
 
Modern frontend workflow
Modern frontend workflowModern frontend workflow
Modern frontend workflow
 
Web components
Web componentsWeb components
Web components
 
Setup nodejs
Setup nodejsSetup nodejs
Setup nodejs
 
Side projects : why it fails
Side projects : why it failsSide projects : why it fails
Side projects : why it fails
 
My webapp workflow
My webapp workflowMy webapp workflow
My webapp workflow
 
Promises in JavaScript
Promises in JavaScriptPromises in JavaScript
Promises in JavaScript
 
Prateek dayal backbonerails-110528024926-phpapp02
Prateek dayal backbonerails-110528024926-phpapp02Prateek dayal backbonerails-110528024926-phpapp02
Prateek dayal backbonerails-110528024926-phpapp02
 

Recently uploaded

Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 

Recently uploaded (20)

Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 

VCR

  • 1. VVCCRR @revathskumar VCR 1 of 17 @revathskumar Sunday 21 September 2014 10:24 PM
  • 2. AABBOOUUTT Rubyist / JavaScripter Yeoman Team Member / @keralarb / @keralajs Works at Blog at Twitter/Github - Google+: @whatznear blog.revathskumar.com @revathskumar +RevathSKumar VCR 2 of 17 @revathskumar Sunday 21 September 2014 10:24 PM
  • 3. WHATZNEAR AARRCCHHIITTEECCTTUURREE VCR 3 of 17 @revathskumar Sunday 21 September 2014 10:24 PM
  • 4. WWEEBBMMOOCCKK Stubbing HTTP requests at low http client lib level Support multiple HTTP library Setting and verifying expectations on HTTP requests Matching requests based on method, URI, headers and body VCR 4 of 17 @revathskumar Sunday 21 September 2014 10:24 PM
  • 5. SSEETTUUPP # Gemfile group :test do gem 'webmock' end # test/test_helper.rb require 'webmock/test_unit' VCR 5 of 17 @revathskumar Sunday 21 September 2014 10:24 PM
  • 6. SSTTUUBBBBIINNGG stub_request(:post, "www.example.com"). with(:body => {:data => {:a => '1', :b => 'five'}}) stub_request(:get, "www.example.com"). to_return(body: "{:data => {:a => '1', :b => 'five'}}", status: 200) VCR 6 of 17 @revathskumar Sunday 21 September 2014 10:24 PM
  • 7. SSTTUUBBBBIINNGG FFRROOMM FFIILLEE # test/test_helper.rb require 'webmock/test_unit' class ActiveSupport::TestCase def load_fixtures name path = File.join(Rails.root, "test", "fixtures", "#{name}.json") return nil unless File.exists?(path) File.read path end end stub_request(:any, "www.example.com"). to_return(:body => load_fixtures('get_products'), :status => 200) VCR 7 of 17 @revathskumar Sunday 21 September 2014 10:24 PM
  • 8. BUT GGOOTT IINNTTOO TTRROOUUBBLLEESS half of my time went to setup and maintain fixture thought of setting up factories, but still problem with structure of JSON response VCR 8 of 17 @revathskumar Sunday 21 September 2014 10:24 PM
  • 9. VVCCRR Make first request to API and save the response replay the saved reponse from second request onwards VCR 9 of 17 @revathskumar Sunday 21 September 2014 10:24 PM
  • 10. VCR 10 of 17 @revathskumar Sunday 21 September 2014 10:24 PM
  • 11. SSEETTUUPP # test/test_helper.rb require 'vcr' require 'webmock/minitest' VCR.configure do | c | c.cassette_library_dir = 'test/fixtures/vcr_cassettes' c.hook_into :webmock end VCR 11 of 17 @revathskumar Sunday 21 September 2014 10:24 PM
  • 12. VCR.use_cassette 'carts/empty', record: :new_episodes do get :show, format: :json assert_response :success assert_template 'show.json' assert_equal 0, JSON.parse(@response.body)['count'] end VCR 12 of 17 @revathskumar Sunday 21 September 2014 10:24 PM
  • 13. test 'remove item' do VCR.use_cassette 'carts/delete', record: :new_episodes do item = {"id"=>"2816", "uri"=>"",..} store = {"id"=>"prestige", "name"=>"Prestige Smart Kitchen"} post :create, format: :json, item: item, store: store_fixture delete :destroy, format: :json, id: 2816, store: 'prestige' assert_response :success assert_template 'show.json' assert_equal 0, JSON.parse(@response.body)['count'] end end VCR 13 of 17 @revathskumar Sunday 21 September 2014 10:24 PM
  • 14. VVCCRR :: RREECCOORRDD MMOODDEESS :once :new_episodes :none :all VCR 14 of 17 @revathskumar Sunday 21 September 2014 10:24 PM
  • 15. RREEQQUUEESSTT MMAATTCCHHIINNGG VCR.use_cassette 'carts/empty', record: :new_episodes, match_requests_on: [:method, :uri, :body] do get :show, format: :json assert_response :success assert_template 'show.json' assert_equal 0, JSON.parse(@response.body)['count'] end :method :uri :body :host :path :headers :query VCR 15 of 17 @revathskumar Sunday 21 September 2014 10:24 PM
  • 16. class UsersControllerTest < ActionController::TestCase def setup VCR.insert_cassette 'users', record: :new_episodes end def teardown VCR.eject_cassette 'users' end ... end VCR 16 of 17 @revathskumar Sunday 21 September 2014 10:24 PM
  • 17. VCR 17 of 17 @revathskumar Sunday 21 September 2014 10:24 PM