正在尝试测试(minitest)调用AWS S3 Bucket copy_to的方法.如何模拟或存根



我们有一个带有copy_for_edit!方法的Attachment模型,它可以帮助Attachment复制自己。附件数据存储在AWS S3存储桶中。我们使用Bucketcopy_to技术在AWS S3服务器上远程执行复制,而无需将数据传输回我们。https://docs.aws.amazon.com/sdk-for-ruby/v3/api/Aws/S3/Object.html#copy_to-实例方法

我正在为这个方法写一个单元测试(在minitest中(,并得到了由Aws::S3::Bucket#copy_to实例方法引起的错误:

Aws::S3::Errors::NoSuchKey: Aws::S3::Errors::NoSuchKey: The specified key does not exist.

我见过无数关于如何存根AWS S3客户端的解决方案,但没有一个Bucket。我肯定我错过了一些简单的东西。代码本身可以在阶段和生产中工作,但在我的开发环境中进行测试时,我显然不想使用AWS S3服务器。但是,即使我将测试环境配置为使用bucket的暂存凭据,这也不起作用(同样的错误(。

我想知道如何在minitest中存根(或类似(Aws::S3::Bucket#copy_to实例方法。

我知道我遗漏了一些细节。我将密切关注这一点,并在需要时进行编辑以添加上下文。

编辑1:测试的简化版本如下:

test '#copy_for_edit! should copy the attachment, excluding some attributes' do
source = attachments(:attachment_simple)  #From an existing fixture.
result = nil
assert_difference(-> { Attachment.count }, 1) do
result = source.copy_for_edit!
end
assert_nil(result.owner)
assert_nil(result.draft_id)
end

将其缩小到实例方法(而不是类方法或属性(有助于我缩小选项范围。我终于把语法弄对了,相信我现在有一个工作测试了。

这基本上是我的解决方案:https://stackoverflow.com/a/29042835/14837782

我不能说我已经禁用了AWS S3Bucket#copy_to方法。实际上,我只是存根了我们自己的方法(copy_attached_file_to(,它最终调用了它,因为我实际上并没有测试那个方法。到了测试这种方法的时候,我可能也会遇到类似的麻烦。尽管这个解决方案可能会以类似的方式截断Bucket。

现在的测试似乎运行正常:

test '#copy_for_edit! should copy the attachment, excluding some attributes' do
source = attachments(:attachment_simple)  # From an existing fixture.
source.stub(:copy_attached_file_to, true) do
result = nil
assert_difference(-> { Attachment.count }, 1) do
result = source.copy_for_edit!
end
assert_nil(result.owner)
assert_nil(result.draft_id)
end
end

相关内容

最新更新