问题描述
因此在我的代码中,我正在尝试测试此方法:
So in my code I have this method I'm trying to test:
# 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
现在,我正在尝试制作两个测试用例-一个用于文件存在的情况,另一个用于文件不存在的情况.
Now I am trying to make two test cases - one for when the file exists, and one for when it doesn't.
关注失败案例.我想对exists?
进行存根处理以引发Aws::S3::Errors::Forbidden
错误,以便file_exists?
方法将返回false.
Focusing on the failure case. I want to stub out the exists?
to raise the Aws::S3::Errors::Forbidden
error so that the file_exists?
method will return 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
运行此测试,我看到以下内容:
Running this test I see this:
wrong number of arguments (given 0, expected 2)
# ./lib/s3_client_builder.rb:48:in `file_exists?'
真的不知道这是怎么回事,因为file_exists?
方法绝对没有2的偶数,我也没有对exists?
方法进行存根.
Really not clear what's going on here, since the file_exists?
method definitely doesn't have an arity of 2 nor does the exists?
method I'm stubbing.
为诊断此问题,我在begin
块中放置了一个断点.我尝试运行<object>.exists?
行,并得到相同的错误.
To diagnose this, I put a breakpoint in the begin
block. I try and run the <object>.exists?
line and get the same error.
推荐答案
事实证明问题出在:
and_raise(
Aws::S3::Errors::Forbidden
)
运行此命令将显示相同的错误:
Running this shows the same error:
raise(Aws::S3::Errors::Forbidden)
这是什么工作:
raise(Aws::S3::Errors::Forbidden.new(nil, nil))
这篇关于Rspec-引发错误时参数数量错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!