go test 测试用例那些事(二) mock (2)

EXPECT()方法返回的就是MockUserMockRecorder看一下我们的例子方法V

// V mocks base method func (m *MockUser) V(idx int, name string) (string, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "V", idx, name) ret0, _ := ret[0].(string) ret1, _ := ret[1].(error) return ret0, ret1 } // V indicates an expected call of V func (mr *MockUserMockRecorder) V(idx, name interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "V", reflect.TypeOf((*MockUser)(nil).V), idx, name) }

返回的*gomock.Call就是最底层的数据结构,保存的所有的自定义参数

type Call struct { t TestHelper // for triggering test failures on invalid call setup receiver interface{} // the receiver of the method call method string // the name of the method methodType reflect.Type // the type of the method args []Matcher // the args origin string // file and line number of call setup preReqs []*Call // prerequisite calls // Expectations minCalls, maxCalls int numCalls int // actual number made // actions are called when this Call is called. Each action gets the args and // can set the return values by returning a non-nil slice. Actions run in the // order they are created. actions []func([]interface{}) []interface{} }

method``methodType保存的方法的信息,mock是从反射字段methodType知道传入参数和返回结果的信息。

args用于保存指定的参数, 是gomock.Any()还是gomock.Eq()等,进行传入参数匹配。

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zyjpww.html