- 在写单元测试的时候遇到这种问题:
=== RUN Testxxx
Testxxx: box_test.go:239: missing call(s) to *xxxmock.Mockxxx.xxx(is anything, is anything, is anything) xxx/xxx/xxx/box_test.go:202
Testxxx: box_test.go:239: aborting test due to missing call(s)
--- FAIL: Testxxx (0.00s)
boxxxx.EXPECT().Updatexxx(gomock.Any(),gomock.Any(),gomock.Any()).Return(int64(2),nil)
//加上AnyTimes() 允许调用0次或多次,就不会出现这个问题了
boxxxx.EXPECT().Updatexxx(gomock.Any(),gomock.Any(),gomock.Any()).AnyTimes().Return(int64(2),nil)
原因:因为测试的代码中多次调用了该函数:Updatexxx,而单元测试中mock的函数中允许调用次数为1,所以会出现这个错误,使用Anytime() 方法,允许调用次数为 0 次或更多次,就不会出现这个错误了。