I am using Go 1.19 on a windows machine with 8 cores, operating system is Windows 10 Pro. I used the mockgen tool to generate the mock.When I debug my test I see the mocked method is recorded when I execute the EXPECT() function.The mocked function is called, but test fails with 'missing call' on the mocked function.
Can anyone please point what I am doing wrong ? Directory Structure : cmd configure.go configure_test.go mocks mock_validator.go validator validator.go user user.go go.mod main.go -- * Contents of main.go package main import ( "localdev/mockexample/cmd" ) func main() { cmd.Configure() } * Contents of configure.go package cmd import ( "fmt" "localdev/mockexample/user" "os" "localdev/mockexample/validator" ) var ( name, password string ) func Configure() { name := os.Args[1] password := os.Args[2] user, err := validate(validator.NewValidator(name, password)) if err != nil { fmt.Printf("%v\n", err) return } fmt.Printf("Credentials are valid. Welcome: %s %s\n", user.FirstName, user.LastName) } func validate(validator validator.Validator) (*user.Data, error) { user, err := validator.ValidateUser() if err != nil { return nil, fmt.Errorf("some thing went wrong. %v", err) } return user, nil } * Contents of validator.go package validator import ( "fmt" "localdev/mockexample/user" ) //go:generate mockgen -destination=../mocks/mock_validator.go -package=mocks localdev/mockexample/validator Validator type Validator interface { ValidateUser() (*user.Data, error) } type ValidationRequest struct { Command string Name string Password string } func (vr ValidationRequest) ValidateUser() (*user.Data, error) { if vr.Name == "bob" && vr.Password == "1234" { return &user.Data{UserID: "123", UserName: "bsmith", FirstName: "Bob", LastName: "Smith"}, nil } return nil, fmt.Errorf("invalid credentials") } func NewValidator(name string, password string) Validator { return &ValidationRequest{Name: name, Password: password} } * Contents of user.go package user type Data struct { UserID string `json:"user_id"` UserName string `json:"user_name"` FirstName string `json:"first_name"` LastName string `json:"last_name"` } * Contents of configure_test.go package cmd import ( "localdev/mockexample/mocks" "localdev/mockexample/user" "os" "testing" "github.com/golang/mock/gomock" ) func TestConfigure(t *testing.T) { t.Run("ConfigureWithMock", func(t *testing.T) { os.Args[1] = "bob" os.Args[2] = "1234" ctrl := gomock.NewController(t) mockValidator := mocks.NewMockValidator(ctrl) //mockValidator.EXPECT().ValidateUser().AnyTimes() // zero more calls, so this will also pass. userData := user.Data{UserID: "testId"} mockValidator.EXPECT().ValidateUser().Return(&userData, nil).Times(1) //(gomock.Any(), gomock.Any()) //(&userData, nil) Configure() }) } Contents of generated mock // Code generated by MockGen. DO NOT EDIT. // Source: localdev/mockexample/validator (interfaces: Validator) // Package mocks is a generated GoMock package. package mocks import ( user "localdev/mockexample/user" reflect "reflect" gomock "github.com/golang/mock/gomock" ) // MockValidator is a mock of Validator interface. type MockValidator struct { ctrl *gomock.Controller recorder *MockValidatorMockRecorder } // MockValidatorMockRecorder is the mock recorder for MockValidator. type MockValidatorMockRecorder struct { mock *MockValidator } // NewMockValidator creates a new mock instance. func NewMockValidator(ctrl *gomock.Controller) *MockValidator { mock := &MockValidator{ctrl: ctrl} mock.recorder = &MockValidatorMockRecorder{mock} return mock } // EXPECT returns an object that allows the caller to indicate expected use. func (m *MockValidator) EXPECT() *MockValidatorMockRecorder { return m.recorder } // ValidateUser mocks base method. func (m *MockValidator) ValidateUser() (*user.Data, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "ValidateUser") ret0, _ := ret[0].(*user.Data) ret1, _ := ret[1].(error) return ret0, ret1 } // ValidateUser indicates an expected call of ValidateUser. func (mr *MockValidatorMockRecorder) ValidateUser() *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ValidateUser", reflect.TypeOf((*MockValidator)(nil).ValidateUser)) } -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/500788c7-e9ba-40e4-b90d-dcfeec904f9an%40googlegroups.com.