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

Setting mocked function's argument value #32

@grindlemire

Description

@grindlemire

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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions