如何模拟aws-sdk gem? [英] How to mock aws-sdk gem?

查看:102
本文介绍了如何模拟aws-sdk gem?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些代码,可以使用aws-sdk gem将文件上传到Amazon S3.显然,它通过HTTP来上传文件.

I have some code that uploads a file to Amazon S3, using the aws-sdk gem. Apparently it does an HTTP put to upload the file.

有没有办法模拟aws-sdk gem的此功能?

Is there a good way to mock this functionality of the aws-sdk gem?

我尝试使用Webmock,但是aws-sdk gem似乎首先执行get latest/meta-data/iam/security-credentials/.似乎使用Webmock可能不是模拟此功能的最佳方法.

I tried using Webmock, but the aws-sdk gem seems to do a get latest/meta-data/iam/security-credentials/ first. It seems that using Webmock may not be the best way to mock this functionality.

在RSpec中工作.

推荐答案

适用于以下版本的AWS开发工具包中,有很多方法可以模拟请求Ruby .特雷弗·罗(Trevor Rowe)最近发布了一篇文章,介绍了该SDK对对象存根的本机支持.任何外部依赖项,例如Webmock.您还可以使用 VCR 之类的工具(链接会将您发送到另一篇博客文章)建立可缓存的集成测试;这样,您可以在需要准确性的情况下针对实时服务进行测试,并在需要速度时避免访问网络.

There are a lot of ways to mock requests in the AWS SDK for Ruby. Trevor Rowe recently posted an article on using the SDK's native support for object stubbing, which does not require any external dependencies like Webmock. You can also use tools like VCR (link will send you to another blog post) to build cacheable integration tests; this way you can test against the live service when you want accuracy and avoid hitting network when you want speed.

关于latest/meta-data/iam/security-credentials/上的get请求,发生这种情况是因为SDK试图查找凭据,如果没有提供凭据,它将作为最后手段检查您是否在EC2实例上运行,从而导致SDK发出额外的HTTP请求.您可以通过简单地提供伪造的静态凭据来避免这种检查,尽管如果使用的是VCR之类的东西,则您将希望在首次运行时提供 valid 凭据.您可以在Trevor撰写的有关凭证管理的另一篇博客文章中了解如何提供静态凭证.(这也应该在开发人员指南和SDK文档中).

Regarding the get request on latest/meta-data/iam/security-credentials/, this happens because the SDK is trying to look up credentials, and, if none are provided, it will check if you are running on an EC2 instance as a last resort, causing the SDK to make an extra HTTP request. You can avoid this check by simply providing bogus static credentials, though if you are using something like VCR, you will want to provide valid credentials for the first run. You can read about how to provide static credentials in another blog post that Trevor wrote on credential management (this should also be in the developer guide and SDK documentation).

这篇关于如何模拟aws-sdk gem?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆