Mock for a builder interface #786
Answered
by
LandonTClipp
mesirendon
asked this question in
Q&A
-
Hi everyone, I'd like to know what's the best way to mock a builder. Let me setup the example: package repository
type (
myBuilder interface {
Set() myBuilder
}
MyRepo struct {
builder myBuilder
}
)
func NewMyRepo(builder myBuilder) *MyRepo {
return &MyRepo { builder: builder }
}
func(r *MyRepo) DoSomething() error {
b := r.builder.Set()
...
return nil
} On the other hand I have my disable-version-string: True
with-expecter: True
packages:
github.com/mesirendon/mydomain/go/module/internal/repository:
config:
all: True
outpkg: "mock"
dir: "{{.InterfaceDir}}/mock"
filename: "{{.InterfaceName}}_mock.go" I'm having problems as the types don't match. In the meantime, I can reduce my coverage by not testing the injection of the actual builder. But it would be nice to have everything tested. Please point me in the right direction. |
Beta Was this translation helpful? Give feedback.
Answered by
LandonTClipp
Jun 7, 2024
Replies: 1 comment 5 replies
-
Can you show the errors you get when testing with the mock? Does mockery correctly set |
Beta Was this translation helpful? Give feedback.
5 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'm pretty sure what's happening here is that your mocks are living in a separate package yet trying to use an unexported type (
myBuilder
) from your original package. This can't work. This is why I generally recommend people generate mocks in the same package.As far as not exposing them publicly, yes you can append
_test
to the filename, or use build tags such that the mocks are generated only when you have atest
tag specified.