Using context.Context to mock API clients
Go code that builds external API clients through a single constructor can be mocked by stashing a mock in context.Context: the constructor (ClientFor) returns the context-held client if present, otherwise builds a real slack.Client. incident.io generates a SlackClient interface from the concrete slack-go struct with interfacer, then a gomock mock via mockgen, both driven by go:generate directives. Because contexts thread through every call, a mock attached at the test's root context reaches all code down the callstack, giving one injection point and a reusable pattern for any client.
Related posts
- Enhancing the Mock Server: A User Interface Approach
Zalando Engineering mediumAdeptZalando's team built a UI on top of mirage.js so product managers and designers can switch which mocked response each API endpoint returns on a staging build, without code changes or redeploys. A React Context provider re-instantiates the mock server on demand, and the selected scenario is kept in session storage under an app-unique key so it survives the page reload that loading a new data set requires. The approach covers edge cases like stale or missing data, but it complements rather than replaces unit and contract tests, and the frontend-defined mocks must be kept in sync with the backend schema. A generalized version now ships as an internal NPM module.
- AI-Powered Testing: Using Docker Model Runner with Microcks for Dynamic Mock APIs
Docker mediumAdeptMicrocks' AI Copilot can generate varied, realistic mock API responses using a local LLM served by Docker Model Runner through its OpenAI-compatible endpoint. The setup pulls a quantized Qwen3 8B model, points Microcks' application.properties at model-runner.docker.internal so containers talk to the runner directly, enables the copilot feature flag, and runs Microcks via Docker Compose. Generated samples can be added to mocked operations and exercised with curl, and declaring the model in compose.yml makes the test setup reproducible.
- Engineering nits: Building a Storybook for Slack Block Kit
incident.io mediumAdeptincident.io built a Storybook-style catalogue for its 70+ Slack modals and similar number of messages by extending its Go modal registration framework so every `modal.Register` call also supplies named example variants built from test fixtures. A `slackpreview` package collects these templates, and an API endpoint with a small Go template renders an index page linking each variant to Slack's Block Kit Builder, which serialises the whole Block Kit payload into a shareable URL. Because examples are cheap to write, they are required for every modal, letting anyone, including non-engineers, preview any Slack UI state within seconds.
- Context Engineering Is Just Writing Things Down
Fly.io mediumAdeptAgent output quality depends more on project and environment context than on model capability, and that context can be built from plain files on disk. OpenClaw's memory design (append-only daily logs, a curated MEMORY.md, hybrid BM25/vector recall weighted 70/30 with diversity re-ranking on SQLite) illustrates the idea. In a Fly.io Sprite, four mechanisms supply that context: CLAUDE.md for conventions, Skills for repeatable workflows like a preflight check, hooks that inject live service and git state, and checkpoints that persist the whole environment. A sample Bun + Hono + Postgres project shows Claude fixing a bug while following the test, logging and error conventions without being told.
- Autonomous testing of services at scale
Meta Engineering mediumAdeptMeta built an integration testing framework for back-end services that reuses production containerization and routing to spin up ephemeral test environments, with per-service boundaries where requests go to local services, in-process Python mocks, in-memory replicas, production (read-only) or are blocked. An autonomous extension mirrors a service's Twine production spec, isolates it via application-level RPC filtering plus an LD_PRELOAD layer intercepting libc connect/sendto/sendmsg, and routes unknown RPCs to a universal mock, letting one-third of services run safely with no human setup. Thrift's reflective IDL lets it auto-generate fuzz harnesses and dependency mocks, and mutating sanitized recorded production requests helps fuzzing reach deep code paths past input validation.