golang / mock

GoMock is a mocking framework for the Go programming language.
Apache License 2.0
9.26k stars 608 forks source link

Mock Embedded Interface in the SAME Package #645

Closed jayaike closed 2 years ago

jayaike commented 2 years ago

Actual behavior A clear and concise description of what the bug is.

I tried to mock an embedded struct in the same package as the source file.

Here is my source file

type Service interface {
    AccountsService
    ModulesService
}

When I try to generate the mock file with the auxfiles flag, I still get an error. I've tried different ways to use the auxfiles flag such as --aux_files service=service.go and --aux_files =service.go.

Expected behavior A clear and concise description of what you expected to happen.

Embedded interfaces in the same package should work

jayaike commented 1 year ago

you should put the full path, including package name

Yuu177 commented 1 year ago

Thank you for your reply

you should put the full path, including package name

How to understand? Is there an example?

I did this error

$ mockgen -source=test01.go -destination=test01_mock.go -package=test -aux_files test=/Users/Cat/code-test/golang/go-test/my_test/test02.go
$ 2022/07/26 19:06:45 Loading input failed: test01.go:5:2: unknown embedded interface ServiceExp

But I use go module name to solve it. Like this:

mockgen -source=test01.go -destination=test01_mock.go -package=test -aux_files testDemo/my_test=test02.go

my file layout

.
├── go.mod
├── go.sum
└── my_test
    ├── test01.go
    ├── test01_mock.go
    └── test02.go

go.mod

module testDemo

go 1.18

require github.com/golang/mock v1.6.0

test01.go

package test

type Service interface {
    Get()
    ServiceExp
}

test02.go

package test

type ServiceExp interface {
    Set()
}
jayaike commented 1 year ago

oh i meant the package path. For example mine was

github.com/xxxx/services/accounts_service.go