Skip to content

fix(write-tests): boundary/wire mocking over interface fakes - #1

Open
ajcarberry wants to merge 1 commit into
mainfrom
fix/write-tests-wire-mocking
Open

fix(write-tests): boundary/wire mocking over interface fakes#1
ajcarberry wants to merge 1 commit into
mainfrom
fix/write-tests-wire-mocking

Conversation

@ajcarberry

Copy link
Copy Markdown
Owner

The write-tests skill taught three things that a real Go CLI testing effort (in a downstream repo) found to be anti-patterns. This brings the skill in line with what actually works.

Changes

  • Interface fakes → wire-level fakes. The old "Layer 2: define a narrow interface, inject a fake" (CommandRunner, assert fake.Calls[0][0]=="nomad") proves the code called a fake, not that it built the right command, and couples tests to a test-only interface. Replaced with: fake the executable on PATH and assert the real argv (shared internal/testutil.FakeExec), or httptest and assert method/path/decoded-body.
  • No more t.Skip() on a missing binary. That is a false-green — if the coverage gate runs go test without building first, the E2E tests silently pass. TestMain builds the binary; a miss is a hard failure. Added the note that subprocess tests don't count toward a package's coverage, so command logic needs in-process Cobra tests (with an os.Stdout capture helper, since commands print via fmt.Printf, not cmd.OutOrStdout()).
  • testify → stdlib testing. The reference mandated testify while the codebases it targets use stdlib. Converted the examples; kept a note to match testify only where a project already standardises on it.
  • Added the "prove the negative" pattern for destructive ops (the fake fails the test on any mutating call) and a Determinism section (map-iteration order and unseeded-RNG bugs — both real defects found in practice).

Kept as-is: table-driven structure, naming, t.Helper(), golden files, TUI state-transition testing, parallelism guidance.

Companion to the downstream ADR that codifies these tenets (homelab-monorepo ADR-0027).

🤖 Generated with Claude Code

The skill taught patterns that a real Go CLI testing effort found to be
anti-patterns. Bring it in line.

- Replace the three-layer strategy (which framed interface-based fakes as a
  legitimate boundary approach) with boundary-first testing: fake the
  executable on PATH and assert the real argv, or httptest and assert the
  method/path/body. Drop the CommandRunner interface pattern — asserting
  "Run was called" proves the code called the fake, not that it built the
  right command, and it couples tests to a test-only interface.
- Stop recommending t.Skip() when the binary is absent — that is a
  false-green (the coverage gate runs `go test` without building first).
  Build the binary in TestMain; a miss is a hard failure. Note that
  subprocess tests don't count toward a package's coverage, so command
  logic needs in-process cobra tests (with an os.Stdout capture helper,
  since commands print via fmt.Printf, not cmd.OutOrStdout).
- Switch examples from testify to stdlib `testing` (t.Fatalf preconditions,
  t.Errorf checks, reflect.DeepEqual, errors.Is/As) to match stdlib
  codebases; keep a note to match testify only where a project already uses it.
- Add the "prove the negative" pattern for destructive ops (fake fails the
  test on any mutating call) and a Determinism section (map-iteration and
  unseeded-RNG bugs).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AfCuqUGhxYxbvo4FgkBtFm
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant