【发布时间】:2014-10-31 00:05:58
【问题描述】:
我正在尝试测试我是否能够捕获这些 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。
谢谢。
【问题讨论】:
-
你在哪一行得到
TypeError? -
../gems/rspec-mocks-3.1.3/lib/rspec/mocks/message_expectation.rb:194
-
您的代码对我来说似乎很好。我唯一担心的是需要 s3client 和 AWS 堆栈库。只是为了实验,尝试将 S3Client 替换为默认知道的任何类 rails(例如任何模型)和 AWS::Errors... 由 StandardError 并运行您的测试。
-
@Alexander,是的,测试很好。如果我将 AWS::Errors::ServerError 替换为 AWS::Errors::RandomError(它不存在),那么我会得到:“未初始化的常量 AWS::Errors::RandomError”。所以听起来 AWS::Errors::ServerError 肯定包括在内。还有其他想法吗?或者也许是一种不同的测试方法?谢谢。
-
@Slowfib:你找到解决方案了吗?我遇到了类似的问题。
标签: ruby amazon-web-services rspec rspec3