A walkthrough of implementing the provider pattern in Rust to make a weather API client testable. The post shows how to refactor a standalone function into a struct-based abstraction (Weatherstack) that encapsulates configuration like API key and base URL, enabling dependency injection for tests. It covers the must_use attribute, converting functions to methods, and using the httpmock crate to test against a mock HTTP server with detailed assertion output.
Table of contents
A provider abstractionBuilding the Weatherstack providerCompulsory return values with must_useget_weather becomes a methodPutting Weatherstack to workBebugging the testChecking the mock’s assertionsPutting it all togetherSort: