
Testing external APIs can be a time-consuming task,VCR is a ruby gem that allows you to record test suite's HTTP interactions and replay them during future test runs for fast, deterministic, accurate tests.
How VCR works?
When we make the first API call the request goes through full request and response cycle. VCR records the API request and response, which it saves as acassette
. In other words, VCR stubs it for future use. When the test is run again, no API call is made. Instead, VCR stubs the response. The test will run much faster as a result.
Installation
VCR works together withwebmock a library for stubbing and setting expectations of HTTP requests. Add the latest version of VCR and Webmock in yourGemfile
. Runbundle install
to update dependencies.
gem'vcr','~> 5.0'gem'webmock','~> 3.7','>= 3.7.6'
Its good to add them in test group gems
Add them as development dependencies if you are developing a gem.
Configuration
VCR implements aconfigure block for different configuration. Add the below block in yourtest_helper.rb
file.
VCR.configuredo|config|config.allow_http_connections_when_no_cassette=falseconfig.cassette_library_dir=File.expand_path('cassettes',__dir__)config.hook_into:webmockconfig.ignore_request{ENV['DISABLE_VCR']}config.ignore_localhost=trueconfig.default_cassette_options={record: :new_episodes}end
- allow_http_connections_when_no_cassette - allows actual full request and response cycle, when no cassette found.
- cassette_library_dir - directory where cassettes are stored.
- hook_into - specifies a request stubbing library
- ignore_request - default is
false
whentrue
VCR makes full request without using cassettes even if they exists. - ignore-localhost - prevent VCR from having any affect on localhost requests
- default_cassette_options - takes a hash that provides defaults for each cassette you use
Testing API calls
We are now ready to start testing APIs calls. For demo, purpose lets useJSONplaceholder a fake API for developers.
classVCRTest<Minitest::Testdeftest_fetch_postVCR.use_cassette("get_post")doresponse=Net::HTTP.get_response(URI('https://jsonplaceholder.typicode.com/posts/1'))assert_equal(200,response.status)endendend
Run the test once and VRC will record incassette_library_dir
you defined in configure block, for our casecassettes/get_post.yml
. When you run the test again no real connection is made making the process very fast. You may realize the speed when you have very many API endpoints to test.
Tips
If you are having issues with your cassettes or you need new data you can deletes the cassettes and a new version will be created.
If you have sensitive information in your responses like API keys. You can filter the keys in your configuration block.
VCR.configuredo|config|config.filter_sensitive_data('<API_KEY>',ENV('API_KEY'))end
If you like the blog, share with friends
Thanks for reading 🎉 😊
Top comments(0)
For further actions, you may consider blocking this person and/orreporting abuse