2

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
williamcodes
  • 6,317
  • 8
  • 32
  • 55

2 Answers2

2

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
williamcodes
  • 6,317
  • 8
  • 32
  • 55
1

I believe your understanding is correct, that if you delete your cassettes, your test suite will hit the actual API and recreate cassettes. Your specs are probably passing because the Wunderground API is returning correct results!

Remember, VCR is a tool that saves us from having to hit a real API to make our specs pass. Hitting the real API is just as valid a way to make our specs pass. The downsides here are time (it takes time to hit external services) and you-must-be-connected-to-the-internet-to-run-your-test-suite.

Do you have any reason to believe that if your test suite hit the real Wunderground API, that your specs should fail?