$30 off During Our Annual Pro Sale. View Details »

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. USING VCR TO MOCK YOUR REQUESTS

    View Slide

  2. SUMMARY
    ● Intro
    ● What is a mock test?
    ● What is VCR?
    ● How to use VCR?
    ● Examples in real life
    ● Bonus: tips & tricks
    ● References

    View Slide

  3. WHAT IS A MOCK TEST?

    View Slide

  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.”

    View Slide

  5. WHAT IS VCR?

    View Slide

  6. WHAT IS VCR?
    “Record your test suite's HTTP interactions and replay them during future test
    runs for fast, deterministic, accurate tests.”

    View Slide

  7. HOW TO USE VCR?

    View Slide

  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?

    View Slide

  9. HOW TO USE VCR?

    View Slide

  10. View Slide

  11. SHOWTIME!

    View Slide

  12. EXAMPLES IN REAL LIFE

    View Slide

  13. EXAMPLES IN REAL LIFE - ELASTIC SITE SEARCH

    View Slide

  14. EXAMPLES IN REAL LIFE - ELASTIC SITE SEARCH

    View Slide

  15. BONUS: TIPS & TRICKS

    View Slide

  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

    View Slide

  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

    View Slide