Search code examples
rubyvcr

How to tell if VCR completed the cassette


How can I tell if VCR has consumed all the recorded data? If the tested program terminates the API accesses or does none at all, the VCR test still completes normally (the test checks for no error raised and a completion message at the end). There are a variety of ways to check state, but having the tested app successfully consume the cassette indicates a lot more than any state artifact I can think of.


Solution

  • Found this in the source code for insert_cassette options:

    @option options :allow_unused_http_interactions [Boolean] If set to false, an error will be raised if a cassette is ejected before all previously recorded HTTP interactions have been used. Defaults to true. Note that when an error has already occurred (as indicated by the $! variable) unused interactions will be allowed so that we don't silence the original error (which is almost certainly more interesting/important).

    and interacts with this eject_cassette option:

    @option options :skip_no_unused_interactions_assertion [Boolean] If true is given, this will skip the "no unused HTTP interactions" assertion enabled by the :allow_unused_http_interactions => false cassette option. This is intended for use when your test has had an error, but your test framework has already handled it.

    So this gets used as:

    it 'successfully accesses the proper APIs' do
      VCR.use_cassette('/some/path', allow_unused_http_interactions: false) do
        expect do
          expect do
            described_class.new(param1, param2).process
          end.to_not raise_exception
        end.to output(/Done!/).to_stdout
      end
    end