directory
Version:
v0.0.0-...-1d28748
Opens a new window with list of versions in this module.
Published: Jul 5, 2023
License: Apache-2.0
Opens a new window with license information.
README
¶
mockgen -destination gomockway/mock_task.go -package mock -source original/task/task.go
生成对应的mock文件 这样就可以去调用了
关键代码:
ctrl := gomock.NewController(t)
defer ctrl.Finish()
newTask: func() task.Task {
mockTask := mock.NewMockTask(ctrl)
//定义了置入的值和返回的值
mockTask.EXPECT().Do(gomock.Any()).Return("", errors.New("return err")).AnyTimes()
return mockTask
},
for _, suit := range testSuits {
t.Run(suit.name, func(t *testing.T) {
taskPool = NewTaskPoolImpl(suit.newTask, suit.size)
// 这里会调用上面中的Do方法 而且将值返回来
err := taskPool.Run(suit.times)
if suit.isErr {
assert.Error(t, err)
} else {
assert.NoError(t, err)
}
})
}
mock的意义
- 觉得写mock的是约定好接口,然后在面向接口做开发的时候能够方便测试,因为不需要接口实际的实现,而是依赖mock的Minimal Implement就可以进行单元测试
- 应该对接口返回什么值敏感,知道接口返回什么值 会对自己的程序造成什么样的影响
Directories
¶
Package mock is a generated GoMock package.
|
Package mock is a generated GoMock package. |
|
|
|
|
Click to show internal directories.
Click to hide internal directories.