golang / mock

GoMock is a mocking framework for the Go programming language.
Apache License 2.0
9.28k stars 610 forks source link

mockgen: does not handle types if mocks are in test package #247

Closed poy closed 5 years ago

poy commented 5 years ago

It looks like mockgen doesn't attach the package name to a type if the package is set (via --package).

Example:

Notice in mock_test.go any usage of User does not have the package name users.

user.go

package users

type User struct {
    Name string
}

type Finder interface {
    FindUser(name string) User
}
mockgen --source=user.go --destination=mock_test.go --package=users_test

mock_test

// Code generated by MockGen. DO NOT EDIT.
// Source: user.go

// Package users_test is a generated GoMock package.
package users_test

import (
    gomock "github.com/golang/mock/gomock"
    reflect "reflect"
)

// MockFinder is a mock of Finder interface
type MockFinder struct {
    ctrl     *gomock.Controller
    recorder *MockFinderMockRecorder
}

// MockFinderMockRecorder is the mock recorder for MockFinder
type MockFinderMockRecorder struct {
    mock *MockFinder
}

// NewMockFinder creates a new mock instance
func NewMockFinder(ctrl *gomock.Controller) *MockFinder {
    mock := &MockFinder{ctrl: ctrl}
    mock.recorder = &MockFinderMockRecorder{mock}
    return mock
}

// EXPECT returns an object that allows the caller to indicate expected use
func (m *MockFinder) EXPECT() *MockFinderMockRecorder {
    return m.recorder
}

// FindUser mocks base method
func (m *MockFinder) FindUser(name string) User {
    ret := m.ctrl.Call(m, "FindUser", name)
    ret0, _ := ret[0].(User)
    return ret0
}

// FindUser indicates an expected call of FindUser
func (mr *MockFinderMockRecorder) FindUser(name interface{}) *gomock.Call {
    return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "FindUser", reflect.TypeOf((*MockFinder)(nil).FindUser), name)
}
poy commented 5 years ago

Related #207