【问题标题】:How to test fog/s3 timing out in rspec?如何在 rspec 中测试雾/s3 超时?
【发布时间】:2014-04-03 10:44:58
【问题描述】:

我有一个小型服务类,它只是在 S3 周围复制一些图像。为了简化这个问题,它看起来有点,但不完全是这样:

class BadgeUpdateJob < Struct.new(:badge)
    def perform
       source_filename = generate_source_filename
       dest_filename   = generate_dest_filename
       storage         = Fog::Storage.new({:provider => 'AWS', :aws_access_key_id => ENV['S3_KEY'], :aws_secret_access_key => ENV['S3_SECRET'] })
       dir = storage.directories.get(s3_bucket_key)
       source_file = dir.files.get(source_filename)
       if source_file
         source_file.copy(dir.key, dest_filename)
       else
         # Notify dev team, something is horribly wrong.
       end
    end  
 end

我可以看到几个明显的错误 - 存储桶可能不存在,源文件可能不存在,但我知道我想如何测试这些 - 我将设置 mocked Fog 以一种导致这些错误发生的方式自然。

我能看到的其他问题是超时……上述 sn-p 中至少有 4 个网络操作,任何一个都可能失败/超时。我想优雅地处理这件事。

有没有“正确”的方式来做到这一点?如果没有,对“最佳”方式有何建议?

【问题讨论】:

    标签: ruby rspec amazon-s3 fog


    【解决方案1】:

    如果 Fog 的模拟层没有实现它,您始终可以使用好的、老式的 RSpec 模拟。

    describe 'BadgeUpdateJob' do
      describe 'timeouts' do
        it 'catches timeouts on authentication' do
          Fog::Storage.stub(:new).and_raise(Excon::Errors::Timeout)
          # ... expect(your code).to handle_it_properly
        end
    
        it 'catches timeouts on directories.get' do
          Fog::Storage::AWS::Directories.any_instance.stub(:get).and_raise(Excon::Errors::Timeout)
          # ...
        end
    
      end
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多