调整断言库依赖及增加gomock示例
parent
81d7b122ea
commit
cf84652c4d
@ -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)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
@ -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)
|
||||||
|
}
|
@ -0,0 +1,10 @@
|
|||||||
|
package gomock
|
||||||
|
|
||||||
|
type User struct {
|
||||||
|
ID int
|
||||||
|
Name string
|
||||||
|
}
|
||||||
|
|
||||||
|
type UserService interface {
|
||||||
|
GetUser(id int) (*User, error)
|
||||||
|
}
|
Loading…
Reference in New Issue