2

これらの AWS 例外をキャプチャできることをテストしようとしています。

begin
  s3_client = S3Client.new
  s3_file = s3_client.write_s3_file(bucket, file_name, file_contents)
rescue AWS::Errors::ServerError, AWS::Errors::ClientError => e
  # do something
end

私のRspec 3コード:

expect_any_instance_of(S3Client).to receive(:write_s3_file).and_raise(AWS::Errors::ServerError)

しかし、このスタブをテストすると、TypeError が発生します。

exception class/object expected

AWS::Errors::ServerError を含める必要がありますか? もしそうなら、どうすればいいですか?aws-sdk-v1 gem を使用しています。

ありがとう。

4

2 に答える 2

0

ポートをビルドしてから、エラーをスローしようとしているスタブ化されたオブジェクトを挿入します。説明させてください:

class ImgService
  def set_client(client=S3Client.new)
    @client = client
  end

  def client
    @client ||= S3Client.new
  end

  def write(bucket, file_name, file_contents)
    begin
      @client.write_s3_file(bucket, file_name, file_contents)
    rescue AWS::Errors::ServerError, AWS::Errors::ClientError => e
      # do something
    end
  end
end

テスト:

describe "rescuing an AWS::Error" do
  before :each do
    @fake_client = double("fake client")
    allow(@fake_client).to receive(:write_s3_file).and_raise(AWS::Errors::ServerError)

    @img_service = ImgService.new
    @img_service.set_client(@fake_client)
  end
  # ...
end
于 2014-11-01T02:34:35.847 に答える