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

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

问题描述

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

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 中工作.

Working in RSpec.

推荐答案

AWS SDK for红宝石.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 之类的东西,您将需要为第一次运行提供有效凭据.您可以在 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天全站免登陆