Rspec - wrong number of arguments when raising error
<p>So in my code I have this method I'm trying to test:</p>

<pre><code>  # 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 =&gt; e
      false
    end
  end
</code></pre>

<p>Now I am trying to make two test cases - one for when the file exists, and one for when it doesn't.</p>

<p>Focusing on the failure case. I want to stub out the <code>exists?</code> to raise the <code>Aws::S3::Errors::Forbidden</code> error so that the <code>file_exists?</code> method will return false.</p>

<p>Here's what my test code looks like:</p>

<pre><code>  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
</code></pre>

<p>Running this test I see this:</p>

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

<p>Really not clear what's going on here, since the <code>file_exists?</code> method definitely doesn't have an arity of 2 nor does the <code>exists?</code> method I'm stubbing.</p>

<p>To diagnose this, I put a breakpoint in the <code>begin</code> block. I try and run the <code>&lt;object&gt;.exists?</code> line and get the same error.</p>