Search code examples
rubyvcr

VCR Not Recording My Response


I'm trying to record a response that the method images makes a request to open a uri and scrape image(s) from a specific blog. However, when I run the testing suite, VCR does not make the yoko/bot_response inside of the cassettes directory and record this. Is there something I've failed to include or am not understanding? (Note: Images makes a request with open-uri)

Example Spec:

describe Yoko do                                                                    
  describe '.images' do                                                                                                                                                  
    context 'when blog is "generic"' do                                             
      let(:proxy) { 'http://119.46.110.17:80' }                                     
      let(:url)   { 'http://electric-firefly.tumblr.com' }                               

      it 'should contain an array of images' do                                     
        VCR.use_cassette 'yoko/bot_response' do                                     
          scraped_images = Yoko.send(:images, url, proxy)                           
          expect(scraped_images).not_to be_empty                                    
      end                                                                         
    end
 end   

VCR Config:

VCR.configure do |vcr|                                                                                                                                                     
  vcr.cassette_library_dir = 'spec/cassettes'
  vcr.hook_into :webmock
end

Solution

  • I figured it out. What was happening was the VCR setup file itself needed to have been required inside of the spec helper. That way way, it's almost as if you were specifying the configuration options inside of the actual test file. This is nice because now you have separation of concerns.

    Inside of Spec Helper:

    require File.expand_path('./support/vcr_setup', __dir__)
    

    VCR Configuration:

    require 'vcr'
    
    VCR.configure do |vcr|                                                                                                                                                     
      vcr.cassette_library_dir = 'spec/cassettes'
      vcr.hook_into :webmock
    end