Search code examples
ruby-on-railsrspecvcr

removing VCR cassette does not cause RSpec to fail


I was having trouble getting VCR to re-record cassettes, so I tried deleting the cassette altogether, and was shocked to find my test suite continued passing. How is this possible? My understanding is that the cassettes are the fixtures used to simulate external API calls. Without them, it should actually hit the API and save a new cassette. Here's my vcr.rb

require 'vcr'

VCR.configure do |c|
  c.cassette_library_dir = 'spec/fixtures/vcr'
  c.hook_into :webmock
  c.default_cassette_options = { serialize_with: :json, record: :once }
  c.debug_logger = File.open(Rails.root.join('log', 'vcr.log'), 'a')
  c.filter_sensitive_data('---WUNDERGROUND_KEY---') { ENV['WUNDERGROUND_KEY'] }
end

Here's one of the mysteriously passing tests:

require 'spec_helper'

describe WeatherMan do
  describe ".current_outdoor_temp" do
    it "returns current outdoor temperature from Wunderground API" do
      VCR.use_cassette('wunderground') do
        temperature = WeatherMan.current_outdoor_temp(10004, 0)
        expect(temperature).to be_a Numeric
      end
    end
  end
end

Solution

  • I was using Rails cache to avoid hitting the API with the same request multiple times in production, and by default Rails uses :file_store which persists to disc rather than to memory and RSpec does not clear it for you. As a result, I was never hitting VCR, WebMock, or my cassettes. The first time I ran the test suite it cached my results in file store and never hit them again. You can restart your whole computer and it doesn't make a difference. This may bite you if you're used to caching in memory. Adding this to spec/spec_helper.rb fixed the problem:

    RSpec.configure do |config|
      ...
      config.before(:each) do
        Rails.cache.clear
      end
      ...
    end