I try to create a mock of a file with an interface imported from another file. I have try with `aux_files` and `imports` but I did non succeed to have a correct mock file. I think I'm missing something.
So I have a `mockgen/main.go` like this :
package main
import (
"log"
o "mockgen/otheri"
)
type bar struct {
a o.Foo
}
func NewBar(a o.Foo) *bar {
return &bar{a}
}
func main() {
var t = NewBar(&o.FunctionStuct{})
if err := t.a.Ask(); err != nil {
log.Fatal(err)
}
}
And the interface imported is in `mockgen/otheri/otheri.go` :
package otheri
import "log"
type Foo interface {
Ask() error
}
type FunctionStuct struct {
}
func (f *FunctionStuct) Ask() error {
log.Println("Hello")
return nil
}
The command I tried is :
mockgen -source main.go -aux_files o=otheri/otheri.go
executed at the same level as the main.go
But my mockgen file is empty....
Does anyone has an idea ? My goal is to mock the interface o.Foo
contains in main.go me without changing my architecture
I need to mock it to test it with unit tests.
The architecture is like this because I follow clean architecture.
Thanks for all
You can generate mocks only for interfaces. So, in your example you should run mockgen for file
mockgen/otheri/otheri.go
because target interface presented where.But as Elias Van Ootegem pointed out, it's a bad practice to have an interface with the struct which conforming it. You should separate interface and implementation. So, it should be something like:
File
/bar/bar.go
File
otheri/otheri.go
File
main.go
And generate a mock
mockgen -source=bar/bar.go -destination=bar/mock/foo_mock.go Foo
Furthermore, follow the rules described in effective go the best way to use your
FunctionStruct
- hide the type in the package:So, the final solution will move interface to a separate package:
File
/foo/foo.go
File
/bar/bar.go
File
otheri/otheri.go
File
main.go
And mockgen:
mockgen -source=foo/foo.go -destination=foo/mock/foo_mock.go Foo