所以在我的代码中我尝试测试这个方法:
# 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 的数量,exists?
也没有我 stub 的方法。
为了对此进行诊断,我在 begin
中放置了一个断点堵塞。我尝试运行 <object>.exists?
行并得到相同的错误。
最佳答案
原来问题出在:
and_raise(
Aws::S3::Errors::Forbidden
)
运行它会显示相同的错误:
raise(Aws::S3::Errors::Forbidden)
这是什么工作:
raise(Aws::S3::Errors::Forbidden.new(nil, nil))
关于ruby-on-rails - Rspec - 引发错误时参数数量错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40775978/