golang / mock

GoMock is a mocking framework for the Go programming language.
Apache License 2.0
9.29k stars 610 forks source link

Mocking s3iface #305

Closed kkesley closed 5 years ago

kkesley commented 5 years ago

Hi, I'm trying to mock s3iface, however, some methods with aws.Context seems to be mocked differently. Here's one example:

mock:

// AbortMultipartUploadWithContext mocks base method
func (m *MockS3API) AbortMultipartUploadWithContext(arg0 context.Context, arg1 *s3.AbortMultipartUploadInput, arg2 ...request.Option) (*s3.AbortMultipartUploadOutput, error) {
    m.ctrl.T.Helper()
    varargs := []interface{}{arg0, arg1}
    for _, a := range arg2 {
        varargs = append(varargs, a)
    }
    ret := m.ctrl.Call(m, "AbortMultipartUploadWithContext", varargs...)
    ret0, _ := ret[0].(*s3.AbortMultipartUploadOutput)
    ret1, _ := ret[1].(error)
    return ret0, ret1
}

interface:

AbortMultipartUploadWithContext(aws.Context, *s3.AbortMultipartUploadInput, ...request.Option) (*s3.AbortMultipartUploadOutput, error)

note the difference in the first arg: context.Context vs aws.Context

it results in type error when passing to my function. Is there any resolution to this?

Note: this is how I generated those mocks: mockgen -destination s3iface/mock_s3iface.go github.com/aws/aws-sdk-go/service/s3/s3iface S3API

vasiliyb commented 5 years ago

am having a similar issue with sqsiface:

$ mockgen -source=~/Code/go/src/github.com/aws/aws-sdk-go/service/sqs/sqsiface/interface.go -destination=mockSQS.go

2019/06/25 11:10:56 Loading input failed: loading package failed

Update: resolved! specify the full path, ~ are not treated correctly

ghost commented 5 years ago

@kkesley the documentation specifies that aws.Context is just an alias to context.Context which is also an interface.

https://docs.aws.amazon.com/sdk-for-go/api/aws/#Context

Do you have repro steps?

kkesley commented 5 years ago

I got this error:

cannot use mockS3 (type mock_s3iface.MockS3API) as type s3iface.S3API in field value: mock_s3iface.MockS3API does not implement s3iface.S3API (wrong type for AbortMultipartUploadWithContext method) have AbortMultipartUploadWithContext(context.Context, s3.AbortMultipartUploadInput, ...request.Option) (s3.AbortMultipartUploadOutput, error) want AbortMultipartUploadWithContext(aws.Context, s3.AbortMultipartUploadInput, ...request.Option) (s3.AbortMultipartUploadOutput, error)

However after experimenting with the project, it turns out that the problem is caused by different version of the iface and aws-sdk. Which is not related to the library.

I'm closing this.

Thanks!