AWS开发工具包:NoMethodError:未定义的方法`put_object'在Rspec测试中

时间:2016-09-17 09:51:49

标签: ruby amazon-web-services rspec aws-sdk s3-bucket

在S3 Buckets上使用AWS SDK并使用Rspec进行测试,即使我做了很多更改,我也经常遇到此错误。

这是代码。

Flow.rb

require 'S3Ops.rb'
require 'aws-sdk'

def putzip(s3,bucket,instance)
  y=File.size('TestZip.zip')
  puts "File size of the test zip is #{ y.to_s}"
  File.open('TestZip.zip','rb') do |file|
    s3.put_object(bucket: bucket, key: instance+'/Test.zip', body: file)
  end
  result=@s3_bucket.list_objects({bucket: @bucket_name})
  z = result.contents[0].size
  puts 'File size of Uploaded file is ' + z.to_s
end

describe 'Test' do
  before(:all) do
    bucket_name = 'testbucket'
    instance_name = 'testinstance'
    s3 = S3Ops.new
    putzip(s3, bucket_name, instance_name)
  end

    **example tests**
end

S3Ops.rb

require 'aws-sdk'

class S3Ops
  def initialize
    @s3 = Aws::S3::Client.new(region: 'ap-southeast-1')
  end

**other functions**
end

错误

Failure/Error: s3.put_object(bucket: bucket, key: instance + '/Test.zip', body: file)    
NoMethodError:
   undefined method `put_object' for #<S3Ops:0x000000020707e0>

我甚至尝试全局化所有变量以增加范围并重新初始化函数内部的s3操作。

require 'S3Ops.rb'
require 'aws-sdk'

def putzip(s3,bucket,instance)
  y=File.size('TestZip.zip')
  puts "File size of the test zip is #{ y.to_s}"
  s3 = S3Ops.new
  File.open('TestZip.zip','rb') do |file|
    s3.put_object(bucket: bucket, key: instance+'/Test.zip', body: file)
  end
  result=@s3_bucket.list_objects({bucket: @bucket_name})
  z = result.contents[0].size
  puts 'File size of Uploaded file is ' + z.to_s
end

describe 'Test' do
  before(:all) do
    @bucket_name = 'testbucket'
    @instance_name = 'testinstance'
    @s3 = S3Ops.new
    putzip(@s3, @bucket_name, @instance_name)
  end

    **example tests**
end

仍然显示同样的错误。纠正错误需要进行哪些更改?

修改

它在示例测试中工作正常,如下所示

it 'checks for zip' do
  result = @s3.list_objects(bucket: bucket)
  puts result.contents[0].key
end
  

输出:TestZip.zip

1 个答案:

答案 0 :(得分:1)

我启动S3Ops的方式是错误的,没有返回语句将指针返回给Flow。因此S3连接失败。

我纠正了它并且现在正在工作。