-
Notifications
You must be signed in to change notification settings - Fork 24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
interface mocking #3
Comments
|
yes that will be expected. In case there are same interface in a structure for example
|
can't agree more! and how is the feature going? @ycydsxy |
+1 |
1 similar comment
+1 |
+1 |
1 similar comment
+1 |
+1, we meet another problem related to interface mocking: If a public interface has a private struct to implement it, the method of the interface can not be mocked. package packageA
type Fooer interface {
Foo(string) string
}
type fooer struct {}
func (f *fooer) Foo(in string) string {
return in
}
func GetFooer() Fooer {
return &a{}
} in another package: package packageB
func TestFoo(t *testing.T) {
PatchConvey("TestFoo", t, func(){
// fooer is not an exported struct, can't mock like this
// Mock((*packageA.fooer).Foo).Return("b").Mock()
out := packageA.GetFooer().Foo("a")
So(out, shouldEqual, "b") // test failed
})
} And if the method is mocked to a function, we can even not express the method's signature... |
@RicardoAGu target := GetMethod(GetFooer(), "Foo")
hook := reflect.MakeFunc(target, ... ).Interface() // build hook with MakeFunc, use reflect.Value as params
Mock(target).To(hook) If you don't need the method's signature, you can just simply remove it like this: |
Is your feature request related to a problem? Please describe.
A quick guide for interface mocking is needed
Describe the solution you'd like
Describe alternatives you've considered
Additional context
GetNestedMethod
seems to work, but not elegant.The text was updated successfully, but these errors were encountered: