Skip to content
This repository has been archived by the owner on Mar 23, 2018. It is now read-only.

VCR Testing

Stephen Balogh edited this page Jul 29, 2016 · 3 revisions

Mocking HTTP Interactions With VCR

For tests that require HTTP interactions to be mocked, we use VCR to "record" a live interaction, and have RSpec play from those recordings while running on Travis. This is crucial, in particular, for any interactions that involve authentication, as Travis (by design) does not have access to any Configula variables.

A simple use of VCR might look something like this:

  1. Write tests that interact with some HTTP web service; if those interactions require credentials or sensitive variables to connect in the first place, then make sure to create corresponding filter_sensitive_data filters in the VCR.configure block of spec/spec_helper.rb. This will allow VCR to hide any occurences of the sensitive variables (stored in ENV) that may be in the resulting cassette, since that cassette will be checked into Git and therefore world-accessible.

  2. Ensure that you namespace your filter strings; VCR will translate between the actual variables and your filter strings by doing a global replace –– it is very easy for there to be collisions, either within the same cassette or even across multiple cassettes, unless we are reasonably certain that a string will never appear anywhere besides where you intend it to. Make sure that not only are all filter strings unique with respect to one another, but also that each is unique in the context of all text and data in any cassette. Therefore, a filter like password is bad! A namespaced string like fda_rest_pass, which preserves the meaning of the filter but keeps it unique and associated with a specific Ichabod feature, is better.

  3. While assigning variables in your spec, make sure to use an OR operator for any variables that you created filters for. The spec must be capable of assigning the ENV var while it exists (during recording, on your local machine), and assigning the filter string while the ENV var does not exist (on Travis). Consider this example:

    let(:fda_rest_user) { ENV['FDA_REST_USER'] || 'fda_rest_user' }
    let(:fda_rest_pass) { ENV['FDA_REST_PASS'] || 'fda_rest_pass' }
    

Make sure that the filter strings being assigned in the OR statement are identical to the ones you assigned in the VCR.configure block. If they aren't, VCR may believe that the conditions being mocked have changed, and therefore that an actual HTTP connection should be attempted, which will likely fail on Travis due to a lack of working credentials. 4. To actually record a cassette, run your specs on a machine that has Git access to Configula. Comb through the resulting cassette, which is stored as a YAML doc in spec/vcr_cassettes, to ensure that you successfully filtered everything sensitive before checking it in.

Clone this wiki locally