Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Using VCR to Mock Your Requests

Using VCR to Mock Your Requests

VCR is a tool that I've been using quite often now to mock test requests. But was a bit tricky to find out how it works, so I want to share the knowledge I've been gathering in the past months to explain a bit about VCR.

Ana Schwendler

July 01, 2021
Tweet

More Decks by Ana Schwendler

Other Decks in Programming

Transcript

  1. SUMMARY • Intro • What is a mock test? •

    What is VCR? • How to use VCR? • Examples in real life • Bonus: tips & tricks • References
  2. WHAT IS A MOCK TEST? “Mocking means creating a fake

    version of an external or internal service that can stand in for the real one, helping your tests run more quickly and more reliably. When your implementation interacts with an object’s properties, rather than its function or behavior, a mock can be used.”
  3. WHAT IS VCR? “Record your test suite's HTTP interactions and

    replay them during future test runs for fast, deterministic, accurate tests.”
  4. • Whenever you want to test a part of code

    that requires an external request, you use .use_cassette method to state that you want VCR to deal with that with a “cassette file” • If there is no fixture yet, VCR is configured by default to create one (this time making a real request) so it can be used for future references. HOW TO USE VCR?
  5. BONUS: TIPS & TRICKS • There is a configuration option

    available to filter sensitive data, preventing it from being written to the cassette file. • If you wish to continue making real requests while testing, VCR also provide other 4 record modes (besides once): new_episodes, none, all and record_on_error