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).本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥15 jupyterthemes 设置完毕后没有效果
- ¥15 matlab图像高斯低通滤波
- ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
- ¥15 钢筋实图交点识别,机器视觉代码
- ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
- ¥50 400g qsfp 光模块iphy方案
- ¥15 两块ADC0804用proteus仿真时,出现异常
- ¥15 关于风控系统,如何去选择
- ¥15 这款软件是什么?需要能满足我的需求
- ¥15 SpringSecurityOauth2登陆前后request不一致