ruby-on-rails - Rspec - 引发错误时参数数量错误

标签 ruby-on-rails ruby amazon-web-services rspec

所以在我的代码中我尝试测试这个方法:

  # 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/

相关文章:

ruby-on-rails - 尽管服务器回复积极,但 Rails 渲染未显示在浏览器中

ruby - 我无法在 Windows 上安装 rmagick gem

ruby - 如何将 IO 对象转换为 Ruby 中的字符串?

ruby - Ruby 的非线性或二次规划求解器

ios - DynamoDB 可以批量删除具有特定分区键的项目吗?

amazon-web-services - 何时可以更改 AWS RDS 中的子网组?

sql - 检查 Rails 中是否不存在记录(来自 ids 数组)?

ruby-on-rails - 消除多个模型的重复路线

ruby-on-rails - 如何在rails中使用elasticsearch

amazon-web-services - 更新 CloudFormation 模板时如何解决“"Incompatible launch template: The network interface' s 设备索引必须为零”问题?