I'm using github.com/aws/aws-sdk-go/aws/request
to get presigned URLs, which I need to upload files to s3 bucket in AWS. I'm writing the test currently, for that I need to mock func (r *Request) Presign(expire time.Duration)
. request.Request
is a struct, not an interface, so i have no idea, how can I mock it.

如何在golang中模拟request.Request
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
2条回答 默认 最新
- doujiao0110 2019-01-21 08:06关注
This isn't directly answering your question, but it might remove the basis of the question altogether.
A neat thing in Go, is that you are able to easily isolate your dependencies using interfaces. If your code, the part that you need to test, is using
Presign
indirectly, it is trivial to test.I.e. create an interface
type HigherLevelAws interface { Upload(file string) error }
and use this interface in your code, along with
Upload
. Then you can easily mock this using e.g. https://godoc.org/github.com/stretchr/testify/mockThe actual implementation, would look something like this
type ActualAwsImpl struct { aws *aws.Client } func (a *ActualAwsImpl) Upload(file string) error { aws.Presign... }
This allows you to test the business part of your code, but of course, still leaves untested code in
ActualAwsImpl
. This untested code, however, may be guaranteed to work by virtue of unit and integration tests in the aws sdk itself. Either way, in my organization, we test this using fake aws services run in docker (e.g. https://github.com/gliffy/fake-s3).本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报