golang/mock

[QUESTION] How to mock a file with interface import from another file

Aornn opened this issue · 1 comments

Aornn commented

Hi !

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 in the same without change my architecture

Thanks for all

It appears there are no interfaces in main, only a struct. mockgen can only generate from an interface.