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).本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥30 这是哪个作者做的宝宝起名网站
- ¥60 版本过低apk如何修改可以兼容新的安卓系统
- ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
- ¥50 有数据,怎么建立模型求影响全要素生产率的因素
- ¥50 有数据,怎么用matlab求全要素生产率
- ¥15 TI的insta-spin例程
- ¥15 完成下列问题完成下列问题
- ¥15 C#算法问题, 不知道怎么处理这个数据的转换
- ¥15 YoloV5 第三方库的版本对照问题
- ¥15 请完成下列相关问题!