mock: interface-only
Exhibit path: docs/examples/codegen/mock/interface-only/
A single interface with two methods, annotated with +glacier:mock. The minimal working case for the mock generator.
Input (in.go)
go
//go:build glacier_codegen_fixture
// Package store defines the data-access interface for the application.
package store
import "context"
// Store is the data-access interface.
//
// +glacier:mock
type Store interface {
// Get retrieves a record by ID.
Get(ctx context.Context, id string) (string, error)
// Put writes a record by ID.
Put(ctx context.Context, id string, value string) error
}Output (out.go)
go
//go:build glacier_codegen_fixture
// Code generated by glaciergen. DO NOT EDIT.
// Source: in.go
package store
import "context"
// MockStore is a generated mock for Store.
type MockStore struct {
// OnGet is called by Get. Set this in your test to control behavior.
OnGet func(ctx context.Context, id string) (string, error)
// OnPut is called by Put. Set this in your test to control behavior.
OnPut func(ctx context.Context, id string, value string) error
}
// Get implements Store.
func (m *MockStore) Get(ctx context.Context, id string) (string, error) {
return m.OnGet(ctx, id)
}
// Put implements Store.
func (m *MockStore) Put(ctx context.Context, id string, value string) error {
return m.OnPut(ctx, id, value)
}What the generator did
- Found the
+glacier:mockmarker on theStoreinterface. - Generated a
MockStorestruct that implementsStore. - Each method has a corresponding
OnXxxrecorder field that test code populates with expected call behavior. - The mock is typed at compile time: no
interface{}parameters or return values appear in the generated code.
Related
nested-interfaces with-types-from-other-package glacier generate