dimanche 24 janvier 2021

go mockgen: how to generate in two packages

I am new in go and I don't understand how to generate mocks in situations when I need to generate it in two packages. I have an interface in package "foo" and described mockgen for it

//go:generate mockgen -source worker.go -destination ./worker_mock_test.go -package foo
//go:generate mockgen -source=worker.go -destination=../bar/mock/worker.go -package=mock

type Worker interface {
    MethodName() (map[string]StructureName, error)
}

and second string creates ../bar/mock/worker.go file but

map[string]StructureName

not doesn't refer to "foo" package. I need something like this:

map[string]foo.StructureName

Also I tried to describe it like this:

//go:generate mockgen -destination=../bar/mock/worker.go -package=mock module_name/internal/foo/worker Worker

where "module_name" is module name from go.mod and it gives me an error:

prog.go:13:2: cannot find module providing package module_name/internal/foo/worker: working directory is not part of a module
prog.go:11:2: cannot find module providing package github.com/golang/mock/mockgen/model: working directory is not part of a module
2021/01/24 14:09:48 Loading input failed: exit status 1
worker.go:4: running "mockgen": exit status 1

Process finished with exit code 1

What am I doing wrong?

Aucun commentaire:

Enregistrer un commentaire