[英]Rspec - wrong number of arguments when raising error
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.现在我正在尝试制作两个测试用例 - 一个用于文件存在时,一个用于文件不存在时。
Focusing on the failure case.专注于失败案例。 I want to stub out the exists?
我想排除exists?
to raise the Aws::S3::Errors::Forbidden
error so that the file_exists?
引发Aws::S3::Errors::Forbidden
错误以便file_exists?
method will return false.方法将返回false。
Here's what my test code looks like:这是我的测试代码的样子:
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?'
Really not clear what's going on here, since the file_exists?
真的不清楚这里发生了什么,因为file_exists?
method definitely doesn't have an arity of 2 nor does the exists?
方法绝对没有 2 的数量,也不exists?
method I'm stubbing.方法我存根。
To diagnose this, I put a breakpoint in the begin
block.为了诊断这一点,我在begin
块中放置了一个断点。 I try and run the <object>.exists?
我尝试运行<object>.exists?
line and get the same error.行并得到相同的错误。
It turns out the problem was with:事实证明,问题在于:
and_raise(
Aws::S3::Errors::Forbidden
)
Running this shows the same error:运行此显示相同的错误:
raise(Aws::S3::Errors::Forbidden)
What does work is this:什么工作是这样的:
raise(Aws::S3::Errors::Forbidden.new(nil, nil))
according to https://docs.aws.amazon.com/sdk-for-ruby/v3/api/Aws/Errors/ServiceError.html#initialize-instance_method根据https://docs.aws.amazon.com/sdk-for-ruby/v3/api/Aws/Errors/ServiceError.html#initialize-instance_method
the three arguments are这三个论点是
The message
is where you can add your own information.该message
是您可以添加自己的信息的地方。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.