Why a test that touches the network is a test you can't trust
An outbound HTTP call, already in this app
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.
def self.call(partner:, http: Net::HTTP)
new(partner, http).call
end
Run that in a test as written and the test makes a request over the internet to whatever URL the partner record holds. The test is then only as reliable as that website's uptime, its response time and this machine's network connection, three things a unit test has no business depending on. This app has one more outbound call, Partners::WebhookTransport, which posts webhook payloads to partners; lesson 5 notes how its tests avoid the network too.
What goes wrong, concretely
A test suite with even one live network call inherits every way a network call can fail that has nothing to do with the code under test. The site can be down. It can be slow enough to blow past FetchSiteIconService::TIMEOUT, which is 5 seconds in the file. It can change its HTML so the icon <link> tag moves. Or CI can have no route to the public internet at all. None of those failures says anything about whether this app's code is correct; they only make the suite flaky, slow or both.
The idea this course is built around
Swap the live network call for a fake one that returns what the test needs, with no bytes crossing a wire. Two widely used Ruby gems do this: WebMock and VCR. Neither is installed in this app; there's no webmock or vcr line in its Gemfile or Gemfile.lock. This course teaches both, from their own documentation, and shows where this app's code already solves the same problem another way, without either gem.
What this course covers
Seven lessons, grounded in this app's code. This lesson introduces Partners::FetchSiteIconService, the outbound-HTTP service the course follows. Lesson 2 covers WebMock's stub_request/to_return syntax and its disable_net_connect! safety net, from its README. Lesson 3 covers VCR's cassette-recording model and how it differs from WebMock, from its README and source. Lesson 4 walks through FetchSiteIconService's fetch, redirect and content-type logic. Lesson 5 shows how this app's spec for that service fakes the network today, with neither gem installed. Lesson 6 covers the GitHub OAuth token exchange, another place network traffic happens, and the gap in how it's tested. Lesson 7 ends with a small, self-contained challenge: a miniature stub registry in plain Ruby.
Keep going with lesson 2
What WebMock does: stub_request, to_return, and disable_net_connect!
WebMock's stub_request, with and to_return syntax, the disable_net_connect! safety net and its RSpec matchers.
A free account also gives you the quizzes, your progress and notes, and the discussion.