Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Testing External APIs with VCR in Rails
Moses Gathuku
Moses Gathuku

Posted on

     

Testing External APIs with VCR in Rails

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'
Enter fullscreen modeExit fullscreen mode

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
Enter fullscreen modeExit fullscreen mode
  • 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 isfalse 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
Enter fullscreen modeExit fullscreen mode

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
Enter fullscreen modeExit fullscreen mode

If you like the blog, share with friends

Thanks for reading 🎉 😊

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Rubyist, Ruby on Rails Software Developer
  • Location
    Nairobi
  • Education
    Bsc Computer Science
  • Work
    Full Stack Ruby on Rails at Freelance
  • Joined

More fromMoses Gathuku

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp