---
title: "Using context.Context to mock API clients"
description: "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."
url: "https://swe.quest/articles/using-context-context-to-mock-api-clients-bfd84ea616b9eed1"
canonical: "https://incident.io/blog/golang-client-mocks"
source: "incident.io"
published: "2021-12-21"
difficulty: "medium"
kind: "How-to"
reading_minutes: 7
tags: ["go","testing"]
---

# Using context.Context to mock API clients

A post on [incident.io](https://swe.quest/sources/incident-io.md), published 2021-12-21, 7 min read, How-to. Original: [https://incident.io/blog/golang-client-mocks](https://incident.io/blog/golang-client-mocks)

## Summary

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.

Written by swe.quest, not by the author.

**Difficulty:** Medium: assumes a working developer; shows how it is built or done

## Topics

- [Go](https://swe.quest/tags/go.md) (Languages)
- [Testing](https://swe.quest/tags/testing.md) (Engineering practices)

## Prerequisites

- Go interfaces ([easy posts on Go](https://swe.quest/tags/go.md?difficulty=easy))
- context.Context
- gomock

## Keywords

context.Context, gomock, mockgen, interfacer, go generate, slack-go, Ginkgo

## Related posts

- [Enhancing the Mock Server: A User Interface Approach](https://swe.quest/articles/enhancing-the-mock-server-a-user-interface-approach-ff47adf3053feadf.md): Zalando Engineering · 2024-04-24 · Medium · 7 min read · Frontend, React, Testing, Developer productivity
  Zalando'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.
- [AI-Powered Testing: Using Docker Model Runner with Microcks for Dynamic Mock APIs](https://swe.quest/articles/ai-powered-testing-using-docker-model-runner-with-microcks-for-dynamic-mock-apis-5407790a30e64f9c.md): Docker · 2025-07-14 · Medium · 4 min read · Testing, LLMs, Containers, Developer tools
  Microcks' AI Copilot can generate varied, realistic mock API responses using a local LLM served by Docker Model Runner through its OpenAI-compatible endpoint.
- [Engineering nits: Building a Storybook for Slack Block Kit](https://swe.quest/articles/engineering-nits-building-a-storybook-for-slack-block-kit-532065c40b7a6e12.md): incident.io · 2023-11-28 · Medium · 5 min read · Developer productivity, Go, Developer tools, Communications
  incident.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.
- [Context Engineering Is Just Writing Things Down](https://swe.quest/articles/context-engineering-is-just-writing-things-down-529d8105d51bf0f4.md): Fly.io · 2026-02-23 · Medium · 5 min read · AI for coding, AI agents, Developer productivity
  Agent output quality depends more on project and environment context than on model capability, and that context can be built from plain files on disk.
- [Autonomous testing of services at scale](https://swe.quest/articles/autonomous-testing-of-services-at-scale-54aa6a1bfbfb71d4.md): Meta Engineering · 2021-10-20 · Medium · 13 min read · Testing, Microservices, RPC & gRPC, Sandboxing & isolation, Developer productivity
  Meta 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.

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
