Image of VCR

Why Use VCR?

Testing attributes that depend on external web requests, for example API calls, may be time consuming and costly. Not only do this calls slow down the evaluation run time, they’ve use up API calls, causing you to hit your rate limit much quicker. This has the potential to, at best, induce you to pause in your testing and restart afterwards and, at worst, violate a feature of your program temporarily.

So, how do we analyze our app’s ability to, let us say, authorize a user with multitasking and Twitter actions such as regain a user’s friends or latest tweets?

There are a number of methods by which we can “stub” or replace external internet requests in our evaluation suite. To find out more about them, you may read this amazing ThoughtBot article. Here, we’ll be focusing specifically on using VCR to solve this problem.

How does VCR Work?

As soon as you configure VCR and set up your test package to use it (more on that in a minute), you conduct your test suite. The first time the tests run, they’ll send outside web requests. Those requests, and their answer will be listed on a”tape”, i.e. a document on your spec directory. Next, every subsequent time you run your test package, VCR, with the support of this WebMock stone, will stub out the external web requests and rather re-play the tape.

Setting Up VCR

1 . Add the VCR gem and the WebMock gem for your Gemfile and bundle install

gem 'webmock'  
gem 'vcr'  

2 . Configure VCR:

Create a folder service inside your spec folder. Create a document, vcr_setup.rb inside of spec/support. Fill it out with the next code:

VCR.configure do |c|  
  #the directory where your cassettes will be saved
  c.cassette_library_dir = 'spec/vcr'
  # your HTTP request service. 
  c.hook_into :webmock
end  

3 . Setup WebMock:

In your spec_helper file, add the following lines:

require 'webmock/rspec'  
WebMock.disable_net_connect!(allow_localhost: true)  

WebMock is a gem that stubs out the outside net requests. VCR utilizes WebMock to prevent external internet requests. VCR instead directs those requests into the cassettes.

4 . Write your tests using VCR. For the purposes of the example, we are going to be analyzing our TwitterApi class. The TwitterApi course initializes to configure the customer, employing the Twitter gem. Here, we’ll take a look at an evaluation for the find_user_for method, which should take in a debate of a username and return the appropriate Twitter User object:

describe TwitterApi do  
  let(:client) {TwitterApi.new}

  describe "#find_user_for" do 
    it "given a username, it returns the correct user object" do 
      VCR.use_cassette('twitter/find_user_for') do
        user = client.find_user_for("sm_debenedetto")
        expect(user.class).to eq(Twitter::User)
        expect(user.username).to eq("sm_debenedetto") 
      end
    end
  end

Let us take a closer look at at the it block that utilizes VCR:

  • First we inform VCR to record the”cassette” (the petition to and response from the Twitter API), to a file spec/vcr/twitter/find_user_for.

  • Then we proceed with our usual RSpec test. Next time that the test suite runs, VCR will use WebMock to take over the internet request and use the data in the cassette file.

And we’re done! You’re ready to stub out internet requests in your test suites easily.