Integrations: WebMock/VCR
This course covers how a Rails application tests its outbound integrations without touching the network. It starts from the four integrations in one repository: site icon fetching, partner webhooks, GitHub OAuth and mail. It compares four places to cut the wire: an injected fake, a library's test mode, WebMock and VCR cassettes. It then covers WebMock stubs and request assertions, VCR record modes and secret filtering, drift between stubs and real services, stubbing many integrations, and a concrete plan for this app.
What you'll learn
- ✓Why real network calls make a test suite flaky, slow, unrepeatable and side-effecting
- ✓How to choose between an injected fake, a library test mode, WebMock and VCR
- ✓Using disable_net_connect!, stub_request, failure stubs and have_requested in WebMock
- ✓Configuring VCR record modes, request matching and filter_sensitive_data
- ✓Mitigating drift between stubs and the real service
- ✓Organizing stubs, failure cases and cassettes across many integrations
The only HTTP this app makes
Before learning tools for stubbing outbound HTTP, it is worth knowing how much outbound HTTP there is. In this repository, a search for every HTTP client returns two files.
Read the full lesson free →Curriculum
7 lessonsThe only HTTP this app makes
Surveys the four integrations in this app and why real network calls make a test suite unreliable.
Four places to cut the wire
Compares four places to cut the wire: injected fakes, library test modes, WebMock and VCR, and what each proves.
WebMock
Covers disable_net_connect!, stub_request with matching, failure stubs like to_timeout, and asserting requests with have_requested.
VCR and cassettes
Explains VCR cassettes, record modes, request matching, secret filtering, and when recording beats writing a stub.
The drift problem
Shows why fakes, stubs and cassettes drift from real services, and four mitigations outside the unit suite.
Stubbing at scale
Addresses stub duplication, happy-path-only tests, cassette sprawl and leaked global state across many integrations.
Putting it together: a plan for this app
Lays out a step-by-step plan for this app: add WebMock, test error paths, log surprises, defer VCR.