【问题标题】:Rspec - wrong number of arguments when raising errorRspec - 引发错误时的参数数量错误
【发布时间】:2017-04-08 03:24:56
【问题描述】:

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

  # 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? 方法也没有。

为了诊断这个问题,我在begin 块中放置了一个断点。我尝试运行<object>.exists? 行并得到同样的错误。

【问题讨论】:

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


    【解决方案1】:

    根据 https://docs.aws.amazon.com/sdk-for-ruby/v3/api/Aws/Errors/ServiceError.html#initialize-instance_method

    三个参数是

    • 上下文(Seahorse::Client::RequestContext)
    • 消息(字符串)
    • 数据 (Aws::Structure)(默认为:Aws::EmptyStructure.new)

    message 是您可以添加自己的信息的地方。

    【讨论】:

      【解决方案2】:

      原来问题出在:

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

      运行它显示相同的错误:

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

      这是做什么的:

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

      【讨论】:

        猜你喜欢
        • 2013-06-22
        • 2018-09-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-03
        • 1970-01-01
        • 2015-02-07
        • 2012-01-07
        相关资源
        最近更新 更多