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
{{ message }}
This repository was archived by the owner on Jun 27, 2023. It is now read-only.
I am trying to mock out a GroupCache(source at https://github.com/golang/groupcache) instance for testing and I am running into issues with the Get() call. GroupCache's Get function returns an error object but places the result of the Get into the third argument, dest, which is of type groupcache.Sink. Is there a way to mock a mutation of an argument in a function using gomock? I have tried using both Do and SetArg but I have been unable to produce the result I am looking for. This is a simplified example of what I am trying to do:
type GroupCache interface {
Get(ctx groupcache.Context, key string, dest groupcache.Sink) error
}
...
func TestGroupCacheMock(t *testing.T){
ctrl := gomock.NewController(t)
mockGroupCache := NewMockGroupCache(ctrl)
mockGroupCache.EXPECT().Get(gomock.Any()).Return(nil).AnyTimes() // This line is where I am unsure of what to do.
...
}
So in this example I want the mocked call to Get from my mocked GroupCache to return nil and set the dest variable to some arbitrary value.
Thanks!
The text was updated successfully, but these errors were encountered:
grindlemire
changed the title
Setting Argument Value in Mock
Setting mocked function's argument value
Apr 19, 2016
SetArg only works when the argument is a pointer. But you aren't probably really trying to set the arg value, but rather invoke a method on that arg value, right? groupcache.Sink has a SetString method (and SetBytes, etc.), and you want the mock to invoke that, right? If so, you want to use Do; pass it a func with a signature matching Get, and then in that func you can call dest.SetString(whatever).
I am trying to mock out a GroupCache(source at https://github.com/golang/groupcache) instance for testing and I am running into issues with the Get() call. GroupCache's Get function returns an error object but places the result of the Get into the third argument, dest, which is of type groupcache.Sink. Is there a way to mock a mutation of an argument in a function using gomock? I have tried using both Do and SetArg but I have been unable to produce the result I am looking for. This is a simplified example of what I am trying to do:
So in this example I want the mocked call to Get from my mocked GroupCache to return nil and set the dest variable to some arbitrary value.
Thanks!
The text was updated successfully, but these errors were encountered: