VHS

a ruby gem for

stubbing every API call in tests

git: joaquin-rivera/vhs

Status quo

git: joaquin-rivera/vhs

XTM

Users API

many API calls

Profiles API

Events API

Problem

git: joaquin-rivera/vhs

 

 

- slow tests

- unreliable results due to network glitches, sandbox is down

- takes time to set up sandboxes + services

- hard to stub API calls in high level (feature) specs

Test suite also does many API calls

Enter VHS

git: joaquin-rivera/vhs

XTM

Users API

Profiles API

Events API

first-time call => records request

repeated calls => replays request

VCR

VCR

VCR

wraps around VCR at typhoeus level

VHS Advantages

git: joaquin-rivera/vhs

Test suite does less API calls

 

- faster tests

- reliable, deterministic results => NO intermittent red builds

- no need to change your specs

- no need to set up sandboxes + services

Usage

git: joaquin-rivera/vhs

gem 'vhs'

bundle

vhs-config-rspec.rb

rake spec # records the VCR cassettes

rake spec # replays existing VCR cassettes

cassettes are YAML files saved under 

spec/fixtures/vcr as default

No stub collision

git: joaquin-rivera/vhs

Turn VHS off to stub yourself

# some spec file

describe YourClass, vhs: false do

context 'also can use rspec tag', vhs: false do

it 'needs VHS to be turned off', vhs: false do

it 'is an spec stubbing RESTApi call' do
  # ... code goes here
  VHS.turn_off
  # stub as needed
  RESTApi.stub(:get, %r{/rest/users/users/ ... })
  VHS.turn_on
  # ... from here on VHS takes over again
end

Dynamic cassettes

git: joaquin-rivera/vhs

Before adding cassettes to git

---
- !ruby/struct:VCR::HTTPInteraction
  request: !ruby/struct:VCR::Request
    method: :post
    uri: http://qi-joaquin-rivera.env.xing.com:3007/rest/search-proxy/templates/xtm/search?rid=internal&consumer=loggedin.web.xtm.search.center
$ vcr-dinamyze-cassettes.rb

dynamize your cassettes

---
- !ruby/struct:VCR::HTTPInteraction
  request: !ruby/struct:VCR::Request
    method: :post
    uri: <%= api_host %>/search-proxy/templates/xtm/search?rid=internal&consumer=loggedin.web.xtm.search.center

now they use AppConfig.sandbox.rest_url

Learn by using

git: joaquin-rivera/vhs

- RESTApi.run_with_time_bandit breaks

- RESTApi::Request.check_response_token breaks

- Request with HTTP Status Code = 0, 4xx, 5xx

- Correct frequency to update cassettes

- Improve patterns when writing specs

- a.s.o...

check code, README and wiki for details 

Benchmark 

git: joaquin-rivera/vhs

without VHS

Finished in 12 minutes 1 second (files took 7.37 seconds to load)
1879 examples, 1 failure, 2 pending

VHS recording
Finished in 11 minutes 11 seconds (files took 8.02 seconds to load)
1884 examples, 79 failures, 2 pending

VHS replaying
Finished in 8 minutes 53 seconds (files took 7.97 seconds to load)
1884 examples, 69 failures, 2 pending

3 minutes gain as of today

but of course we are not green yet ;-)

Contact me

joaquin-rivera/vcr

Joaquin Rivera @ xing

 

- You need help to use VHS

- Ideas

- Opinions

- Questions

- Pull Requests

joaquin-rivera/vhs

git: 

VHS a ruby gem for stubbing every API call in tests

By Joaquin Rivera Padron

VHS a ruby gem for stubbing every API call in tests

VHS is a ruby gem that records every API calls in your tests, then it replays them every time you run your tests.

  • 1,995