Replay test suite's HTTP interactions as WebMock stubs in RSpec.
Install the gem and add to the application's Gemfile by executing:
bundle add rspec-cassetteIf bundler is not being used to manage dependencies, install the gem by executing:
gem install rspec-cassetteRequire the RSpec integration in your spec_helper.rb or rails_helper.rb:
require "rspec/cassette/rspec_helper"
RSpec::Cassette.configure do |config|
config.cassettes_dir = "spec/fixtures/cassettes"
config.default_match_on = %i[method uri]
config.allow_http_connections_when_no_cassette = false
config.ignore_localhost = true
config.ignore_hosts = ["selenium-hub", "chromedriver"]
config.ignore_request { |request| URI(request.uri).port == 9200 }
endHelper method style:
describe "API client" do
it "fetches users" do
use_cassette("users/index")
# ...
end
endMetadata style:
it "fetches users", use_cassette: "users/index" do
# ...
endVCR metadata compatibility style:
it "fetches users", vcr: true do
# cassette name is auto-generated from example descriptions
end
it "matches request body", vcr: { cassette_name: "users/index", match_requests_on: %i[method uri body] } do
# ...
endTo pass match options per example:
it "matches body", use_cassette: "users/index", cassette_options: { match_on: %i[method uri body] } do
# ...
endBy default, rspec-cassette now blocks outgoing HTTP connections when no cassette is active:
RSpec::Cassette.configure do |config|
config.allow_http_connections_when_no_cassette = false
endAllow passthrough for localhost or specific hosts:
RSpec::Cassette.configure do |config|
config.ignore_localhost = true
config.ignore_hosts = ["selenium-hub", "chromedriver"]
endUse ignore_request for custom conditions:
RSpec::Cassette.configure do |config|
config.ignore_request do |request|
URI(request.uri).port == 9200
end
endBefore:
it "fetches users" do
VCR.use_cassette("users/index") do
# ...
end
endAfter:
it "fetches users", use_cassette: "users/index" do
# ...
end| Option | Default | Description |
|---|---|---|
cassettes_dir |
spec/fixtures/cassettes |
Base directory for cassette files |
default_match_on |
[:method, :uri] |
WebMock matchers to apply |
allow_http_connections_when_no_cassette |
false |
Allow real HTTP when no cassette is active |
ignore_localhost |
false |
Allow localhost requests while other outgoing requests are blocked |
ignore_hosts |
[] |
Allow specific hosts while other outgoing requests are blocked |
ignore_request |
none | Register predicate blocks to allow specific requests |
After checking out the repo, run bundle install to install dependencies. Then run bundle exec rspec to execute the tests.
Bug reports and pull requests are welcome on GitHub at https://github.com/ydah/rspec-cassette.
The gem is available as open source under the terms of the MIT License.