所以在我的代码中我有这个方法,我试图测试:
# checks if a file already exists on S3
def file_exists?(storage_key)
begin
s3_resource.bucket(@bucket).object(storage_key).exists?
rescue Aws::S3::Errors::Forbidden => e
false
end
end
现在我正在尝试制作两个测试用例 - 一个用于文件存在时,另一个用于何时不存在。
专注于失败案例。我想将exists?
存根,以引发Aws::S3::Errors::Forbidden
错误,以便file_exists?
方法返回false。
这是我的测试代码的样子:
it "returns false if the file doesn't already exist" do
allow_any_instance_of(Aws::S3::Object).to receive(:exists?).and_raise(
Aws::S3::Errors::Forbidden
)
expect(instance.file_exists?('foo')).to be false
end
运行此测试我看到了:
wrong number of arguments (given 0, expected 2)
# ./lib/s3_client_builder.rb:48:in `file_exists?'
真的不清楚这里发生了什么,因为file_exists?
方法肯定没有2的arity,exists?
方法我也没有。
为了诊断这个,我在begin
块中放了一个断点。我尝试运行<object>.exists?
行并得到相同的错误。
答案 0 :(得分:17)
事实证明问题在于:
and_raise(
Aws::S3::Errors::Forbidden
)
运行此操作会显示相同的错误:
raise(Aws::S3::Errors::Forbidden)
这是什么工作:
raise(Aws::S3::Errors::Forbidden.new(nil, nil))
答案 1 :(得分:1)
这三个参数是
message
是您可以添加自己的信息的地方。