Skip to content

samber/cc-skills-golang / skills/golang-google-wire

golang-google-wire

Verified

Compile-time dependency injection in Golang using google/wire — wire.NewSet, wire.Build, wire.Bind (interface→concrete), wire.Struct, wire.Value, wire.InterfaceValue, wire.FieldsOf, cleanup functions, //go:build wireinject injector files, and generated wire_gen.go. Apply when using or adopting google/wire, when the codebase imports `github.com/google/wire`, or when wiring an application graph at compile time via `wire.Build`. For runtime DI with reflection, see `samber/cc-skills-golang@golang-uber-dig` skill.

FileKindSize
SKILL.mdskill md10.9 KB
evals/evals.jsonconfig15.6 KB
references/advanced.mdmarkdown7.3 KB
references/recipes.mdmarkdown6.9 KB
references/testing.mdmarkdown5.5 KB

references/testing.md

View on GitHub
# Testing — google/wire

Wire generates plain Go constructor calls, so tests work directly on the constructor layer — no container API to learn.

## Table of Contents

- [Unit Tests: Plain Constructor Injection](#unit-tests-plain-constructor-injection)
- [Test Injectors: Swapping Providers](#test-injectors-swapping-providers)
- [Passing Mocks as Injector Arguments](#passing-mocks-as-injector-arguments)
- [CI: Detecting Stale `wire_gen.go`](#ci-detecting-stale-wire_gengo)
- [Testing Interface Bindings](#testing-interface-bindings)
- [Table-Driven Tests Without Wire](#table-driven-tests-without-wire)

## Unit Tests: Plain Constructor Injection

The generated code has no wire dependency. Test constructors directly:

```go
func TestUserService_GetUser(t *testing.T) {
    mockStore := &MockUserStore{users: map[int64]*User{1: &User{ID: 1, Name: "Alice"}}}
    cache := newTestRedis(t)
    svc := service.NewUserService(mockStore, cache)

    u, err := svc.GetUser(context.Background(), 1)
    require.NoError(t, err)
    assert.Equal(t, "Alice", u.Name)
}
```

Pass mocks directly as constructor arguments. No wire, no container, no file to generate. This is the idiomatic approach for unit tests.

## Test Injectors: Swapping Providers

For integration or component tests where you want the full wired graph but with selected dependencies replaced, create a test-only injector in a `_test.go` file.

```go
// app_test.go
//go:build wireinject

package main

import (
    "testing"
    "github.com/google/wire"
)

// TestSet replaces real infra with in-memory fakes
var TestSet = wire.NewSet(
    NewTestConfig,
    NewInMemoryUserStore,
    wire.Bind(new(repo.UserStore), new(*InMemoryUserStore)),
    NewTestRedis,
)

func InitTestApp(t *testing.T) (*App, func(), error) {
    wire.Build(TestSet, service.ServiceSet, NewApp)
    return nil, nil, nil
}
```

```go
// app_integration_test.go
//go:build !wireinject  // compiles when the wireinject tag is NOT set

package main

func TestApp_GetUser(t *testing.T) {
    app, cleanup, err := InitTestApp(t)
    require.NoError(t, err)
    defer cleanup()

    // test against the fully-wired app with fake dependencies
    u, err := app.GetUser(context.Background(), 1)
    require.NoError(t, err)
    assert.NotNil(t, u)
}
```

Run `wire ./...` to generate `wire_gen.go` — the test injector is included because the `_test.go` file is compiled as part of the package during `go test`.

**Key pattern from upstream best practices:** Prefer creating a test-only provider set over passing mocks as injector arguments (though both work). The set approach keeps the test injector composable.

## Passing Mocks as Injector Arguments

An alternative to a test set: pass the mock directly as an injector parameter. Wire treats it as a pre-built provider.

```go
//go:build wireinject

func InitTestApp(store repo.UserStore) (*App, func(), error) {
    wire.Build(config.ConfigSet, service.ServiceSet, NewApp)
    return nil, nil, nil
}

// Test
func TestApp(t *testing.T) {
    mock := &MockUserStore{}
    app, cleanup, err := InitTestApp(mock)
    require.NoError(t, err)
    defer cleanup()
    // ...
}
```

Use this form when you only need to replace one or two dependencies and a full `TestSet` is overkill.

## CI: Detecting Stale `wire_gen.go`

If `wire_gen.go` is not regenerated after a provider change, CI builds pass but the graph is wrong. Enforce freshness in CI:

```bash
# Option 1: re-run wire and check for diffs
wire ./...
git diff --exit-code -- '**/wire_gen.go'
```

```yaml
# .github/workflows/ci.yml
- name: Check wire_gen.go is up-to-date
  run: |
    go install github.com/google/wire/cmd/wire@v0.7.0
    wire ./...
    git diff --exit-code -- '**/wire_gen.go'
```

```bash
# Option 2: use wire check (verifies graph without regenerating)
wire check ./...
```

`wire check` exits non-zero if the graph is inconsistent but does **not** update `wire_gen.go`. Use it for a fast graph-validity check without modifying files.

## Testing Interface Bindings

`wire.Bind` can be used in test sets to bind a fake to the same interface:

```go
// Fake implements the same interface as the real provider
type FakeMailer struct{ sent []string }
func (f *FakeMailer) Send(to, body string) error { f.sent = append(f.sent, to); return nil }

var TestMailerSet = wire.NewSet(
    NewFakeMailer,
    wire.Bind(new(notification.Mailer), new(*FakeMailer)),
)

var TestSet = wire.NewSet(
    TestMailerSet,
    realServiceSet,  // everything else is real
)
```

This keeps the test injector narrow — only the Mailer is faked; the rest of the graph is real.

## Table-Driven Tests Without Wire

Wire is an initialization tool. Once the object graph is built, table-driven tests on individual services need no wire involvement:

```go
func TestUserService(t *testing.T) {
    cases := []struct {
        name  string
        id    int64
        users map[int64]*User
        want  string
        err   bool
    }{
        {"found", 1, map[int64]*User{1: &User{Name: "Alice"}}, "Alice", false},
        {"not found", 99, nil, "", true},
    }
    for _, tc := range cases {
        t.Run(tc.name, func(t *testing.T) {
            svc := service.NewUserService(&MockUserStore{users: tc.users}, nil)
            u, err := svc.GetUser(context.Background(), tc.id)
            if tc.err { require.Error(t, err); return }
            assert.Equal(t, tc.want, u.Name)
        })
    }
}
```

Wire has no role here — the injector was only needed to build the object graph in `main` (or in an integration test). Unit tests construct dependencies directly.
reverse-engineer-anythingmorluto/reaReverse engineer native, managed, Electron/JavaScript, packaged, firmware, and browser targets with REA, and build agent environments (AgentEnv bundles) of web applications from a live instance or a capture, and of Linux desktop apps with a task from a request. Use shipped-artifact or requested runtime evidence to explain features, compare versions, decompile code, or guide a reconstruction. Skip REA for ordinary source-repository architecture analysis.Stars: 472kUpdated Claude CodeCodexVerifiedInstall
diagnosing-superpowersobra/superpowersUse when a superpowers session went wrong and your human partner wants to know why — repeated work, ignored plans, stumbles, poor results, a skill that didn't fire, "it took too long", "why is it so expensive", "what is it doing" — or wants to build a bug report for the superpowers maintainers, for the current session or a past one identified by id or path, on any harness.Stars: 322kUpdated Claude CodeCodexVerifiedInstall
dispatching-parallel-agentsobra/superpowersUse when facing 2+ independent tasks that can be worked on without shared state or sequential dependenciesStars: 322kUpdated Claude CodeCodexVerifiedInstall
executing-plansobra/superpowersUse when executing an implementation plan in the current session as the implementer yourself — your human partner chose inline execution, or no subagent tool is availableStars: 322kUpdated Claude CodeCodexVerifiedInstall