Junior Track Roadmap

Testing: WebMock & VCR

This course covers how to keep tests from making live HTTP calls, using an outbound service in this app as the example. It explains why network calls make a test suite flaky and slow. It covers WebMock's stub_request, to_return and disable_net_connect!, and VCR's cassettes and record modes. It reads Partners::FetchSiteIconService and the hand-rolled fake_http helper its spec passes through an http: keyword argument. It also looks at how OmniAuth's test mode skips the GitHub token exchange, and ends with a small stub registry challenge in plain Ruby.

What you'll learn

  • ✓Why live network calls make tests unreliable
  • ✓Stubbing requests with WebMock's stub_request, with and to_return
  • ✓Making unstubbed requests fail with disable_net_connect!
  • ✓Recording and replaying HTTP with VCR cassettes and record modes
  • ✓Faking Net::HTTP through a dependency-injection seam
  • ✓How OmniAuth test mode replaces the GitHub token exchange in specs
Free preview · Lesson 1 3 min read

Why a test that touches the network is a test you can't trust

app/services/partners/fetch_site_icon_service.rb, which ships in this app today, does something none of the earlier testing courses touched. It opens a Net::HTTP connection to a partner's own website, downloads the HTML, looks for an icon link, and downloads that too.

Read the full lesson free →

Curriculum

7 lessons
Type to start searching...