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.

47b7b2966a70bb8247f6123e469871e7?s=128

Ana Schwendler

July 01, 2021
Tweet

Transcript

  1. USING VCR TO MOCK YOUR REQUESTS

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

    What is VCR? • How to use VCR? • Examples in real life • Bonus: tips & tricks • References
  3. WHAT IS A MOCK TEST?

  4. 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.”
  5. WHAT IS VCR?

  6. WHAT IS VCR? “Record your test suite's HTTP interactions and

    replay them during future test runs for fast, deterministic, accurate tests.”
  7. HOW TO USE VCR?

  8. • 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?
  9. HOW TO USE VCR?

  10. None
  11. SHOWTIME!

  12. EXAMPLES IN REAL LIFE

  13. EXAMPLES IN REAL LIFE - ELASTIC SITE SEARCH

  14. EXAMPLES IN REAL LIFE - ELASTIC SITE SEARCH

  15. BONUS: TIPS & TRICKS

  16. 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
  17. REFERENCES • https://github.com/anaschwendler/vcr_example • https://dev.to/marleyspoon/using-vcr-to-mock-your-requests-3127 • https://github.com/vcr/vcr • https://circleci.com/blog/how-to-test-software-part-i-mocking-stubbing-and-con tract-testing/

    • https://fabioperrella.github.io/10_tips_to_help_using_the_VCR_gem_in_your_r uby_test_suite.html