samber/cc-skills-golang / skills/golang-stretchr-testify
golang-stretchr-testify
VerifiedComprehensive guide to stretchr/testify for Golang testing. Covers assert, require, mock, and suite packages in depth. Use when writing tests with testify, creating mocks, setting up test suites, or choosing between assert and require. Covers testify assertions, mock expectations, argument matchers, call verification, suite lifecycle, and advanced patterns like Eventually, JSONEq, and custom matchers. Apply when the codebase imports github.com/stretchr/testify.
| File | Kind | Size |
|---|---|---|
| SKILL.md | skill md | 7.2 KB |
| evals/evals.json | config | 11.9 KB |
| references/mock.md | markdown | 3.1 KB |
references/mock.md
View on GitHub# testify/mock — Reference
Mock interfaces to isolate the unit under test. Embed `mock.Mock`, implement methods with `m.Called()`, and always verify with `AssertExpectations(t)`.
## Quick example
```go
type MockSender struct { mock.Mock }
func (m *MockSender) Send(ctx context.Context, to string, msg Message) error {
return m.Called(ctx, to, msg).Error(0)
}
func TestOrderService_Place(t *testing.T) {
is := assert.New(t)
m := new(MockSender)
m.On("Send", mock.Anything, "buyer@example.com", mock.AnythingOfType("Message")).Return(nil)
err := NewOrderService(m).Place(context.Background(), order)
is.NoError(err)
m.AssertExpectations(t)
}
```
## Defining a mock
```go
type NotificationSender interface {
Send(ctx context.Context, to string, msg Message) error
BatchSend(ctx context.Context, recipients []string, msg Message) (int, error)
}
type MockNotificationSender struct { mock.Mock }
func (m *MockNotificationSender) Send(ctx context.Context, to string, msg Message) error {
return m.Called(ctx, to, msg).Error(0)
}
func (m *MockNotificationSender) BatchSend(ctx context.Context, recipients []string, msg Message) (int, error) {
args := m.Called(ctx, recipients, msg)
return args.Int(0), args.Error(1)
}
```
## Argument matchers
```go
// mock.Anything — matches any value
m.On("Send", mock.Anything, mock.Anything, mock.Anything).Return(nil)
// mock.AnythingOfType — matches by type name
m.On("Send", mock.Anything, mock.AnythingOfType("string"), mock.Anything).Return(nil)
// mock.MatchedBy — custom predicate
m.On("Send", mock.Anything, mock.MatchedBy(func(to string) bool {
return strings.HasSuffix(to, "@example.com")
}), mock.Anything).Return(nil)
```
## Call modifiers
```go
m.On("Send", mock.Anything, mock.Anything, mock.Anything).Return(nil).Once() // exactly 1 call
m.On("Send", mock.Anything, mock.Anything, mock.Anything).Return(nil).Times(3) // exactly 3 calls
m.On("Send", mock.Anything, mock.Anything, mock.Anything).Return(nil).Maybe() // optional
// Side effects
m.On("Send", mock.Anything, mock.Anything, mock.Anything).
Run(func(args mock.Arguments) {
msg := args.Get(2).(Message)
t.Logf("mock received: %s", msg.Subject)
}).Return(nil)
```
## Different returns per call
```go
// First call returns error, second succeeds (retry testing)
m.On("Send", mock.Anything, mock.Anything, mock.Anything).Return(errors.New("timeout")).Once()
m.On("Send", mock.Anything, mock.Anything, mock.Anything).Return(nil).Once()
```
## Removing expectations
```go
call := m.On("Send", mock.Anything, mock.Anything, mock.Anything).Return(nil)
call.Unset()
m.On("Send", mock.Anything, mock.Anything, mock.Anything).Return(errors.New("fail"))
```
## Verification
```go
m.AssertExpectations(t) // verify all expectations
m.AssertCalled(t, "Send", mock.Anything, "buyer@example.com", mock.Anything) // specific call made
m.AssertNotCalled(t, "BatchSend", mock.Anything, mock.Anything, mock.Anything) // specific call NOT made
m.AssertNumberOfCalls(t, "Send", 2) // exact call count
```
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| finishing-a-development-branchobra/superpowersUse when implementation is complete, all tests pass, and you need to decide how to integrate the work | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |
| systematic-debuggingobra/superpowersUse when encountering any bug, test failure, or unexpected behavior, before proposing fixes | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |
| test-driven-developmentobra/superpowersUse when implementing any feature or bugfix, before writing implementation code | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |
| skill-creatoranthropics/skillsCreate new skills, modify and improve existing skills, and measure skill performance. Use when users want to create a skill from scratch, edit, or optimize an existing skill, run evals to test a skill, benchmark skill performance with variance analysis, or optimize a skill's description for better triggering accuracy. | Stars: 198k | Updated | Claude CodeCodex | Verified | Install |