[go-nuts] mockgen tool not calling mocked function

2022-10-14 Thread Ron Khera


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 {UserID: "123", UserName: 
"bsmith", FirstName: "Bob", LastName: "Smith"}, nil
}
return nil, fmt.Errorf("invalid credentials")
}
func NewValidator(name string, password string) Validator {
return {Name: name, Password: password}
}
* Contents of user.go
package user
type Data struct {
UserIDstring `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(, nil).Times(1) 
//(gomock.Any(), gomock.Any()) //(, 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 := {ctrl: ctrl}
mock.recorder = {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, 

Re: [go-nuts] Re: go:embed question

2022-10-14 Thread 'Dan Kortschak' via golang-nuts
On Fri, 2022-10-14 at 00:17 -0700, esimov wrote:
> . Now, that's not the case when you are running a package using the
> following command: "go run github.com/user/package@latest" for
> example

Can you give an example of a real package where this doesn't work?

-- 
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/2af5f60f43a9c2f169ab5d888f20ac96d18ea826.camel%40kortschak.io.


[go-nuts] Re: go:embed question

2022-10-14 Thread esimov
Let me rephrase it: so normally when you are embedding an external file 
with the go://embed directive, when you are running the "go run" command it 
will embed it at compile time. Now, that's not the case when you are 
running a package using the following command: "go run 
github.com/user/package@latest" for example. Hope that's clear now.

On Thursday, October 13, 2022 at 8:22:26 PM UTC+3 Marcel Huijkman wrote:

> I find this question a bit hard to understand. I think it has to do with 
> go run and then a URL, which can't be done.
> Can you rephrase it?
>
> On Wednesday, October 12, 2022 at 9:37:17 AM UTC+2 esi...@gmail.com wrote:
>
>> I know that //go:embed directive reads the content of the embedable file 
>> at compile time. Now if that's the case I wondering if you are running a Go 
>> executable with "go run" at the same time while you are downloading like go 
>> run github.com/user/projectname@latest (which should embed an external 
>> file) why the file is not embedded?
>>
>> Normally if the file you want to embed does not exits, the compiler will 
>> throw you an error telling that "no matching files found". Why is not the 
>> case when you are running without a prior download? 
>>
>> I'm asking this because if I'm running the application normally the file 
>> is getting embedded correctly, but if I'm trying to build it on-the-fly 
>> with go run this is not happening? 
>>
>

-- 
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/21d532ad-af6d-4276-b68e-3c98fc17d0b0n%40googlegroups.com.