Agent skill
gomock
GoMock for generating and using mock implementations of Go interfaces
Install this agent skill to your Project
npx add-skill https://github.com/majiayu000/claude-skill-registry/tree/main/skills/other/other/gomock
SKILL.md
Skill: gomock
What I do
I provide expertise in using GoMock to create and manage mock implementations of interfaces for unit testing. I focus on defining expectations, verifying call sequences, and isolating components for reliable BDD-style testing.
When to use me
- When writing unit tests for components that depend on interfaces
- When verifying complex interactions between a service and its repository
- When simulating error conditions or specific return values from dependencies
Core principles
- Isolation: Use mocks to test the logic of a single component without invoking its real dependencies.
- Expectation setting: Clearly define what calls are expected, what they should return, and how many times they should occur.
- Verification: Ensure that all expected calls were made by verifying the controller state at the end of the test.
- Readability: Keep mock setups concise and readable to maintain the focus on the behaviour being tested.
Patterns & examples
Basic mock setup:
func TestUserService(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish() // Required to verify expectations
mockRepo := mocks.NewMockUserRepository(ctrl)
svc := NewUserService(mockRepo)
// Set expectations
mockRepo.EXPECT().
Get(gomock.Eq(1)).
Return(&User{ID: 1, Name: "Alice"}, nil).
Times(1)
user, err := svc.FindUser(1)
// Assertions...
}
Using argument matchers:
Use gomock.Any() when the specific value doesn't matter, or custom matchers for complex validation.
mockRepo.EXPECT().Save(gomock.Any()).Return(nil)
Stubbing behavior with DoAndReturn:
mockRepo.EXPECT().Get(gomock.Any()).DoAndReturn(func(id int) (*User, error) {
if id == 0 {
return nil, errors.New("not found")
}
return &User{ID: id}, nil
})
Ordering calls:
gomock.InOrder(
mockRepo.EXPECT().Get(1).Return(u, nil),
mockRepo.EXPECT().Save(u).Return(nil),
)
Anti-patterns to avoid
- ❌ Over-mocking: Do not mock internal implementation details. Only mock at interface boundaries.
- ❌ Ignoring ctrl.Finish(): Forgetting to call
Finish()(or use the built-in cleanup in newer Go versions) means failed expectations won't cause the test to fail. - ❌ Brittle expectations: Avoid overly strict ordering or call counts unless they are critical to the system's correctness.
KB Reference
~/vaults/baphled/3. Resources/Knowledge Base/AI Development System/Skills/Testing-BDD/Gomock.md
Related skills
bdd-workflow: For structuring tests that describe system behaviourginkgo-gomega: For using mocks within a Ginkgo test suitecode-generation: For automating the creation of mock files usingmockgengolang: For principles of interface design and composition
Recommended Agent Skills
Expand your agent's capabilities with these related and highly-rated skills.
agent-ops-spec
Manage specification documents in .agent/specs/. Use when user provides requirements, acceptance criteria, or feature descriptions that need to be tracked and validated against implementation.
agent-ops-state
Maintain .agent state files. Use at session start, after meaningful steps, and before concluding: read/update constitution/memory/focus/issues/baseline consistently.
agent-ops-spec
Manage specification documents in .agent/specs/. Use when user provides requirements, acceptance criteria, or feature descriptions that need to be tracked and validated against implementation.
agent-ops-testing
Test strategy, execution, and coverage analysis. Use when designing tests, running test suites, or analyzing test results beyond baseline checks.
agent-ops-testing
Test strategy, execution, and coverage analysis. Use when designing tests, running test suites, or analyzing test results beyond baseline checks.
agent-ops-state
Maintain .agent state files. Use at session start, after meaningful steps, and before concluding: read/update constitution/memory/focus/issues/baseline consistently.
Didn't find tool you were looking for?