Search code examples
ruby-on-railsrubyvcr

VCR fails with DB (cassettes table doesn't exist) error when running tests


While using VCR with my Rails 3.1 app on Ruby 1.9, running rake test with an existing cassette produces:

Error: test_#create_returns_created_account_upon_successful_creation(AccountServiceTest) ActiveRecord::StatementInvalid: Mysql2::Error: Table 'test_db.vcr_cassettes_account_service_create' doesn't exist: DELETE FROM vcr_cassettes_account_service_create

My VCR config in my test_helper.rb:

 VCR.configure do |c|
   c.allow_http_connections_when_no_cassette = true
   c.cassette_library_dir = 'test/fixtures/vcr_cassettes'
   c.hook_into :fakeweb
 end

Applicable Test::Unit code:

require 'test_helper'

class AccountServiceTest < ActiveSupport::TestCase
  test '#create returns created account upon successful creation' do
    VCR.use_cassette('account_service_create') do
      created_account = AccountService.new.create({name: 'honeybadger'})
      assert_equal 'honeybadger', created_account.name
    end
  end
end

FYI, I'm also using DatabaseCleaner; not sure if there's a conflict there. Anyone seen this before?

NOTE: This passes when the VCR cassette does not exist yet


Solution

  • I think what's happening is that something is trying to rebuild or revert everything in your test/fixtures directory; And its trying to revert everything in vcr_cassettes as well, which doesn't exist.

    Moving my vcr_cassettes to a directory outside of test/fixtures solved this for me.

    1) Delete the test/fixtures/vcr_cassettes directory

    2) And change your test_helper.rb to:

    c.cassette_library_dir = 'test/vcr_cassettes'