VCR gem是录制和重放HTTP请求的工具。VCR使用了WebMock库,WebMock可以拦截HTTP请求并进行模拟。

安装

bundle add rspec-rails vcr --group "development, test"

配置 spec/rails_helper.rb

require 'vcr'

VCR.configure do |c|
  c.cassette_library_dir = 'spec/cassettes'
  c.hook_into :webmock
  c.configure_rspec_metadata!
end

RSpec.configure do |c|
  # so we can use `:vcr` rather than `:vcr => true`;
  # in RSpec 3 this will no longer be necessary.
  c.treat_symbols_as_metadata_keys_with_true_values = true
end