Skip to content
This repository was archived by the owner on Jun 27, 2023. It is now read-only.

Setting mocked function's argument value #32

Closed
grindlemire opened this issue Apr 19, 2016 · 2 comments
Closed

Setting mocked function's argument value #32

grindlemire opened this issue Apr 19, 2016 · 2 comments

Comments

@grindlemire
Copy link

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!

@grindlemire grindlemire changed the title Setting Argument Value in Mock Setting mocked function's argument value Apr 19, 2016
@dsymonds
Copy link
Contributor

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).

@grindlemire
Copy link
Author

Ah I see! I had an incorrect assumption on how Do worked. Thanks so much!

kucherenkovova pushed a commit to kucherenkovova/mock that referenced this issue Jul 12, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants