调整断言库依赖及增加gomock示例
This commit is contained in:
parent
81d7b122ea
commit
cf84652c4d
15
go.mod
15
go.mod
@ -3,8 +3,15 @@ module go-study
|
||||
go 1.20
|
||||
|
||||
require (
|
||||
github.com/brildum/testify v0.0.0-20151105045740-d05693e2e501 // indirect
|
||||
github.com/marusama/cyclicbarrier v1.1.0 // indirect
|
||||
golang.org/x/exp v0.0.0-20240119083558-1b970713d09a // indirect
|
||||
golang.org/x/sync v0.5.0 // indirect
|
||||
github.com/marusama/cyclicbarrier v1.1.0
|
||||
github.com/stretchr/testify v1.9.0
|
||||
golang.org/x/exp v0.0.0-20240119083558-1b970713d09a
|
||||
golang.org/x/sync v0.5.0
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||
go.uber.org/mock v0.4.0 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
)
|
||||
|
11
gomock/demo.go
Normal file
11
gomock/demo.go
Normal file
@ -0,0 +1,11 @@
|
||||
package gomock
|
||||
|
||||
func GetUserName(us UserService, id int) (string, error) {
|
||||
// 使用依赖注入进来的具体结构体方法,通常为需要mock的方法
|
||||
user, err := us.GetUser(id)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return user.Name, nil
|
||||
}
|
59
gomock/demo_test.go
Normal file
59
gomock/demo_test.go
Normal file
@ -0,0 +1,59 @@
|
||||
package gomock
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
|
||||
"go.uber.org/mock/gomock"
|
||||
)
|
||||
|
||||
func TestGetUserName(t *testing.T) {
|
||||
ctrl := gomock.NewController(t)
|
||||
defer ctrl.Finish()
|
||||
|
||||
mockUserService := NewMockUserService(ctrl)
|
||||
|
||||
// 定义测试用例
|
||||
testCases := []struct {
|
||||
name string
|
||||
userID int
|
||||
mockSetup func()
|
||||
expected string
|
||||
err error
|
||||
}{
|
||||
{
|
||||
name: "valid user",
|
||||
userID: 1,
|
||||
mockSetup: func() {
|
||||
mockUserService.EXPECT().
|
||||
GetUser(1).
|
||||
Return(&User{ID: 1, Name: "Alice"}, nil)
|
||||
},
|
||||
expected: "Alice",
|
||||
err: nil,
|
||||
},
|
||||
{
|
||||
name: "user not found",
|
||||
userID: 2,
|
||||
mockSetup: func() {
|
||||
mockUserService.EXPECT().
|
||||
GetUser(2).
|
||||
Return(nil, errors.New("user not found"))
|
||||
},
|
||||
expected: "",
|
||||
err: errors.New("user not found"),
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
tc.mockSetup()
|
||||
|
||||
result, err := GetUserName(mockUserService, tc.userID)
|
||||
|
||||
assert.Equal(t, tc.expected, result)
|
||||
assert.Equal(t, tc.err, err)
|
||||
})
|
||||
}
|
||||
}
|
54
gomock/mock_user.go
Normal file
54
gomock/mock_user.go
Normal file
@ -0,0 +1,54 @@
|
||||
// Code generated by MockGen. DO NOT EDIT.
|
||||
// Source: gomock/user.go
|
||||
//
|
||||
// Generated by this command:
|
||||
//
|
||||
// mockgen -source=gomock/user.go -destination=gomock/mock_user.go -package=gomock
|
||||
//
|
||||
|
||||
// Package gomock is a generated GoMock package.
|
||||
package gomock
|
||||
|
||||
import (
|
||||
reflect "reflect"
|
||||
|
||||
gomock "go.uber.org/mock/gomock"
|
||||
)
|
||||
|
||||
// MockUserService is a mock of UserService interface.
|
||||
type MockUserService struct {
|
||||
ctrl *gomock.Controller
|
||||
recorder *MockUserServiceMockRecorder
|
||||
}
|
||||
|
||||
// MockUserServiceMockRecorder is the mock recorder for MockUserService.
|
||||
type MockUserServiceMockRecorder struct {
|
||||
mock *MockUserService
|
||||
}
|
||||
|
||||
// NewMockUserService creates a new mock instance.
|
||||
func NewMockUserService(ctrl *gomock.Controller) *MockUserService {
|
||||
mock := &MockUserService{ctrl: ctrl}
|
||||
mock.recorder = &MockUserServiceMockRecorder{mock}
|
||||
return mock
|
||||
}
|
||||
|
||||
// EXPECT returns an object that allows the caller to indicate expected use.
|
||||
func (m *MockUserService) EXPECT() *MockUserServiceMockRecorder {
|
||||
return m.recorder
|
||||
}
|
||||
|
||||
// GetUser mocks base method.
|
||||
func (m *MockUserService) GetUser(id int) (*User, error) {
|
||||
m.ctrl.T.Helper()
|
||||
ret := m.ctrl.Call(m, "GetUser", id)
|
||||
ret0, _ := ret[0].(*User)
|
||||
ret1, _ := ret[1].(error)
|
||||
return ret0, ret1
|
||||
}
|
||||
|
||||
// GetUser indicates an expected call of GetUser.
|
||||
func (mr *MockUserServiceMockRecorder) GetUser(id any) *gomock.Call {
|
||||
mr.mock.ctrl.T.Helper()
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetUser", reflect.TypeOf((*MockUserService)(nil).GetUser), id)
|
||||
}
|
10
gomock/user.go
Normal file
10
gomock/user.go
Normal file
@ -0,0 +1,10 @@
|
||||
package gomock
|
||||
|
||||
type User struct {
|
||||
ID int
|
||||
Name string
|
||||
}
|
||||
|
||||
type UserService interface {
|
||||
GetUser(id int) (*User, error)
|
||||
}
|
@ -7,7 +7,7 @@ import (
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
|
||||
"github.com/brildum/testify/assert"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestPool(t *testing.T) {
|
||||
|
Loading…
Reference in New Issue
Block a user