Rspec - wrong number of arguments when raising an error

So, in my code, I have this method that I am trying to verify:

# 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'm trying to make two test cases: one for when the file exists, and the other for when it does not work.

Focus on failure. I want to drown exists? to raise the error Aws::S3::Errors::Forbidden so that the file_exists? method file_exists? returned false.

This is 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:

  wrong number of arguments (given 0, expected 2) # ./lib/s3_client_builder.rb:48:in `file_exists?' 

It is actually unclear what is going on here since the file_exists? method file_exists? definitely does not have arity of 2 and does not execute the exists? method exists? .

To diagnose this, I set a breakpoint in the begin block. Am I trying to run the <object>.exists? and get the same error.

+5
source share
1 answer

Turns out the problem was this:

 and_raise( Aws::S3::Errors::Forbidden ) 

Performing this operation shows the same error:

 raise(Aws::S3::Errors::Forbidden) 

What kind of work is this:

 raise(Aws::S3::Errors::Forbidden.new(nil, nil)) 
+10
source

Source: https://habr.com/ru/post/1260248/


All Articles