Search⌘ K
AI Features

VCR and RSpec

Understand how to use VCR with RSpec to efficiently test external service integrations in Rails apps. This lesson covers VCR configuration options, managing cassettes, and handling HTTP request stubbing to ensure accurate and stable tests.

VCR and RSpec

We can use RSpec metadata to specify that any RSpec it or describe block uses a VCR cassette. The configuration goes into another RSpec support file:

Ruby
VCR.configure do |c|
c.cassette_library_dir = "spec/cassettes"
c.hook_into :webmock
c.configure_rspec_metadata!
c.ignore_localhost = true
end

VCR options

Here, four options are specified:

First option

First, the directory where VCR is going to place its cassette files (which can be anything we want, but something like spec/cassettes is customary).

Second option

The second option involves the HTTP stub library. VCR handles the creation and use of the cassette files, but it subcontracts the actual stubbing of HTTP calls to another ...