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

Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024The Digital Insurer
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...gurkirankumar98700
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsRoshan Dwivedi
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilV3cube
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 

Recently uploaded (20)

Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 

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