Golang Testing
ASecurityUnit test the service by depending on an interface, not a concrete DB client. Your service should receive a repository or store interface: ```go type UserStore interface { GetByID(ctx context.Context, id string) (*User, error) } type Service struct { store UserStore } func NewService(store UserStore) *Service { return &Service{store: store} } ``` Then in tests, replace the real DB with a mock or fake implementation: ```go type mockUserStore struct { user *User err error } func (m *mockUserSt...
- 584 stars
- 0 votes
- 0 copies
- 2 views
- Added September 5, 2026
Works with
Security analysis
100/100Pro scans all 11 files and shows the line behind each finding
npx -y skills add HoangNguyen0403/agent-skills-standard --skill golang-testing --agent claude-codeAre you the author of Golang Testing?
Add the live security badge to your README. It updates with every re-scan.
[](https://www.skillsdirectory.com/skills/hoangnguyen0403-golang-testing-3c2aa5b1)Unit test the service by depending on an interface, not a concrete DB client.
Your service should receive a repository or store interface:
```go
type UserStore interface {
GetByID(ctx context.Context, id string) (*User, error)
}
type Service struct {
store UserStore
}
func NewService(store UserStore) *Service {
return &Service{store: store}
}
```
Then in tests, replace the real DB with a mock or fake implementation:
```go
type mockUserStore struct {
user *User
err error
}
func (m *mockUserStore) GetByID(ctx context.Context, id string) (*User, error) {
return m.user, m.err
}
```
Use table-driven tests so you can cover success and failure paths cleanly:
```go
func TestService_GetUser(t *testing.T) {
t.Parallel()
tests := []struct {
name string
store UserStore
wantErr bool
}{
{
name: "returns user",
store: &mockUserStore{user: &User{ID: "1"}},
wantErr: false,
},
{
name: "propagates store error",
store: &mockUserStore{err: errors.New("db failed")},
wantErr: true,
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
svc := NewService(tt.store)
_, err := svc.GetUser(context.Background(), "1")
if (err != nil) != tt.wantErr {
t.Fatalf("got err=%v, wantErr=%v", err, tt.wantErr)
}
})
}
}
```
This keeps the unit test fast and deterministic because you are testing service logic, not database integration.
If you want to test the real DB wiring, keep that as a separate integration test.
Files in this skill
- eval-1.baseline.md
- eval-1.with-skill.md
- eval-2.baseline.md
- eval-2.with-skill.md
- eval-3.baseline.md
- eval-3.with-skill.md
- trigger-1.md
- trigger-2.md
- trigger-3.md
- trigger-4.md
Attribution
Related skills
View all in development →Comments
Loading comments…
Newsletter
Get the weekly list of safe new skills, and the ones we pulled.