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

Rails after 5 years
Rails after 5 yearsRails after 5 years
Rails after 5 yearsRob Dawson
 
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/2013Hstry
 
WebRTC Conference Prototype Implementation
WebRTC Conference Prototype ImplementationWebRTC Conference Prototype Implementation
WebRTC Conference Prototype ImplementationHenry Hung Yu Chen
 
Server Core with Morden C
Server Core with Morden CServer Core with Morden C
Server Core with Morden CChun Hao Wang
 
Vagrant and Configuration Management
Vagrant and Configuration ManagementVagrant and Configuration Management
Vagrant and Configuration ManagementGareth Rushgrove
 
One vagrantfile to rule them all
One vagrantfile to rule them allOne vagrantfile to rule them all
One vagrantfile to rule them allBert Van Vreckem
 
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 ...Puppet
 

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

cellphone repair chennai
cellphone repair chennaicellphone repair chennai
cellphone repair chennailoiusmatley
 
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 seriesmattnlis
 
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 needdneubert
 
All About Workplace Electrical Safety
All About Workplace Electrical SafetyAll About Workplace Electrical Safety
All About Workplace Electrical SafetyCurrent Solutions PC
 
Transistor history and types
Transistor history and typesTransistor history and types
Transistor history and typesram kumar
 
‫What are conductors_and_insulators - نسخة
‫What are conductors_and_insulators - نسخة‫What are conductors_and_insulators - نسخة
‫What are conductors_and_insulators - نسخةsalman248
 
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 WorkplaceDarabi
 
Inductors, how they work
Inductors, how they workInductors, how they work
Inductors, how they workcallr
 
The squirrel-cage induction motor
The squirrel-cage induction motorThe squirrel-cage induction motor
The squirrel-cage induction motorTharindu Lakshan
 
Electrical Power Systems Induction motor
Electrical Power Systems Induction motorElectrical Power Systems Induction motor
Electrical Power Systems Induction motorMubarek Kurt
 
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 Safetymrmackenzie
 
New Electrical Safety Present2009
New Electrical Safety Present2009New Electrical Safety Present2009
New Electrical Safety Present2009Nestor Fraga
 
Microphone types and characteristics essay
Microphone types and characteristics essayMicrophone types and characteristics essay
Microphone types and characteristics essayJoshGmanMcLean
 

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

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

Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusZilliz
 
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 ...apidays
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
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, ...Angeliki Cooney
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxRustici Software
 
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 TerraformAndrey Devyatkin
 
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 connectorsNanddeep Nachan
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistandanishmna97
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 
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 SavingEdi Saputra
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesrafiqahmad00786416
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyKhushali Kathiriya
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...apidays
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native ApplicationsWSO2
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDropbox
 
"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 ...Zilliz
 

Recently uploaded (20)

Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
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 ...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
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, ...
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
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
 
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
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
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
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
"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 ...
 

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