You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I recently encountered an issue when trying to use mockery to generate a mock for an interface in package x that contains an unexported method.
// x/x.gopackage x
typeFoointerface {
Bar()
baz()
}
Generating a mock for this interface using
mockery --with-expecter --exported --dir x --name Foo --output mocks
// y/y.gopackage y
funcexample(t x.Foo) {
t.Bar()
}
// y/y_test.gofuncTest_example(t*testing.T) {
foo:=mocks.NewFoo(t)
example(foo) // <-- error "cannot use foo (variable of type *mocks.Foo) as x.Foo value in argument to example: *mocks.Foo does not implement x.Foo (missing method baz)"
}
This is a limitation of in how we define a type to satisfy the interface. According to this post, if the type embeds the target interface, it satisfies the compiler. For example:
// Code generated by mockery v2.14.0. DO NOT EDIT.package mocks
import mock "github.com/stretchr/testify/mock"// Foo is an autogenerated mock type for the Foo typetypeFoostruct {
x.Foo// <-- manually adding this fixes the issue.
mock.Mock
}
Therefore, my question is, would it be possible to embed the original interface in the mock if the interface has unexported methods?
Mockery Version
v2.14.0.
Golang Version
go 1.18.3
Installation Method
go install
The text was updated successfully, but these errors were encountered:
You can embed the original interface to ensure mocked struct implements it.But you can not override it with mocked function. Because it is not exported, the mthod of the same name in the mock file is not the same as that method in the interface
I recently encountered an issue when trying to use mockery to generate a mock for an interface in
package x
that contains an unexported method.Generating a mock for this interface using
This is a limitation of in how we define a type to satisfy the interface. According to this post, if the type embeds the target interface, it satisfies the compiler. For example:
Therefore, my question is, would it be possible to embed the original interface in the mock if the interface has unexported methods?
Mockery Version
v2.14.0.
Golang Version
go 1.18.3
Installation Method
The text was updated successfully, but these errors were encountered: