Skip to content

Mocking

Six techniques for isolating external dependencies. Covers HTTP client interfaces, RoundTripper transport mocking, testify/mock, function variable injection, and package-level var swap.

Chapters

  • chapter-10-before-hook-pattern.md — The before Hook Pattern — a typed fixture function returns fresh test state for each case
    Source: chapters/chapter-10-before-hook-pattern/
  • chapter-11-http-client-interface-mock.md — HTTP Client Interface Mock — define an HTTPClient{ Do() } interface and stub its single method
    Source: chapters/chapter-11-http-client-interface-mock/
  • chapter-12-roundtripper-mock.md — RoundTripper Mock — implement http.RoundTripper to mock at the transport layer without changing production types
    Source: chapters/chapter-12-roundtripper-mock/
  • chapter-13-testify-mock-interfaces.md — testify/mock for Interfaces — embed mock.Mock, use On().Return() for interface mock expectations
    Source: chapters/chapter-13-testify-mock-interfaces/
  • chapter-14-function-variable-injection.md — Function Variable Injection — store json.Marshal, http.NewRequest as struct fields for test seams
    Source: chapters/chapter-14-function-variable-injection/
  • chapter-15-package-level-var-swap.md — Package-Level Var Swap — override a package variable and restore with defer for minimal seam injection
    Source: chapters/chapter-15-package-level-var-swap/
  • chapter-32-interface-extraction-3rd-party.md — Interface Extraction from Third-Party Deps — wrap a concrete library behind a small interface, inject through the constructor, swap with testify/mock in tests
    Source: chapters/chapter-32-interface-extraction-3rd-party/

Running the code

Each chapter is a standalone Go module. To run tests for a chapter:

cd <source-directory>
go test -v ./...