如何测试AWS S3获取对象方法?

时间:2015-09-24 15:05:18

标签: ruby-on-rails ruby amazon-web-services amazon-s3

我使用aws sdk for ruby​​从存储桶中检索对象然后读取它。我的代码类似于:

def import_from_s3 

  #initiate the client
  s3 = Aws::S3::Client.new({
      region:            region,
      access_key_id:     key_id,
      secret_access_key: secret
  })

  #Get the object
  resp = s3.get_object(bucket: bucket, key: key)
end

我的问题是如何在不嘲笑的情况下测试此方法?

2 个答案:

答案 0 :(得分:2)

您不需要(甚至不应该尝试)来测试"。这不是由您的代码实现的,您应该假设它已经过测试并且可以正常运行。对于方法#get_object,您有两种选择。你要么不测试它,因为它只是#import_from_s3周围的薄包装;或者你可以对其返回值做出断言/期望。

答案 1 :(得分:1)

以下是有关如何进行此操作的文档。

Stubbing the aws client response

我使用了默认存根,它运行得很好。

Aws.config[:s3] = {stub_responses: {get_object: {body: StringIO.new("XYZ")}}}