Slide 1

Slide 1 text

USING VCR TO MOCK YOUR REQUESTS

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

WHAT IS A MOCK TEST?

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

WHAT IS VCR?

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

HOW TO USE VCR?

Slide 8

Slide 8 text

● 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?

Slide 9

Slide 9 text

HOW TO USE VCR?

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

SHOWTIME!

Slide 12

Slide 12 text

EXAMPLES IN REAL LIFE

Slide 13

Slide 13 text

EXAMPLES IN REAL LIFE - ELASTIC SITE SEARCH

Slide 14

Slide 14 text

EXAMPLES IN REAL LIFE - ELASTIC SITE SEARCH

Slide 15

Slide 15 text

BONUS: TIPS & TRICKS

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

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