【问题标题】:Upgraded to Rails 4, and now getting "wrong number of arguments (1 for 2+)" error升级到 Rails 4,现在出现“参数数量错误(1 代表 2+)”错误
【发布时间】:2019-06-07 17:43:11
【问题描述】:

我最近将我的应用程序从 Rails 3 升级到 Rails 4,并且正在尝试运行规范测试。我认为过去可以正常工作的代码(在我在这里之前)突然抛出一个错误。

错误:

1) Admin::ReviewsController while logged in #index should get index
     Failure/Error: stub_search("product")
     ArgumentError:
       wrong number of arguments (1 for 2+)
     # ./spec/support/searchkick_stub.rb:5:in `stub_search'
     # ./spec/controllers/admin/reviews_controller_spec.rb:8:in `block (3 levels) in <top (required)>'

这里是 searchkick_stub.rb:

module SearchkickStub
  def stub_search(model)
    klass = model.to_s.camelize.constantize
    klass.any_instance.stub(:reindex) { true }
    klass.stub(:search) do |term, options|
      options ||= {}
      @search_term ||= term
      @search_params ||= options.dup

      response = {
    'hits' => {
          'total' => 0,
          'hits' => [],
    }
      }
      result_opts = {
    # per Searchkick::Query.new
    page: [options[:page].to_i, 1].max,
    per_page: (options[:limit] || options[:per_page] || 100000).to_i,
    padding: [options[:padding].to_i, 0].max,
    load: options[:load].nil? ? true : options[:load],
    # per Searchkick::Query.execute
    includes: options[:include] || options[:includes],
    json: !options[:json].nil?,
      }
      Searchkick::Results.new(klass, response, result_opts)
    end

    # Code that reindexes Products will reindex their Inventories too.
    stub_search(:inventory) if model == :product
  end
end

stub_search 的签名显然是针对单个参数的,而不是像错误声明那样的两个或更多。

这是我们在 reviews_controller_spec.rb 中使用 stub_search 的地方

describe ReviewsController do
  include SearchkickStub

  before do
    stub_search(:product)
    ...
  end
end

【问题讨论】:

  • 你能给整个 SearchkickStub 吗?跟踪表明问题出在 searchkick_stub.rb 的第 5 行。在黑暗中拍摄,但产生错误的可能是对 stub_search 的不同调用?
  • 另外,与您的直接问题无关,但即使是 Rails 4 现在也有些旧了。 Rails 4.2 本身很快就要报废了。如果您的目标是让您的应用程序保持最新状态,您可能需要考虑跳过一步直接使用 Rails 5 甚至 6。
  • 已编辑。嗯。第五行调用“klass.stub(:search)”。谢谢,也许这会有所帮助。
  • 相信我,我知道。但我们只是试图从 Wheezy 升级到 Jessie,以及 ruby​​ 和 rails。就我们现在能够升级的东西而言。

标签: ruby-on-rails-4 rspec


【解决方案1】:

想通了。根据https://github.com/rspec/rspec-rails/issues/941,问题在于:spec_helper.rb 中的require 'minitest/autorun'。添加此行以删除以下警告:

Warning: you should require 'minitest/autorun' instead.
Warning: or add 'gem "minitest"' before 'require "minitest/autorun"'

但事实证明,您只需要 Gemfile 中的gem "minitest"(即使它已经安装,作为其他东西的依赖项,并且出现在 Gemfile.lock 中)。

【讨论】:

    【解决方案2】:

    我认为问题更多来自 ruby​​ 升级。关于如何处理块参数,解释器可能已经发生了变化。搜索方法在您的代码中采用 2 个参数:termoptions。但它只用一个参数调用:"product"

    options 在该块的第一行使用options ||= {} 设置为默认值,因此在 1.9.3 中不传递选项可能不是问题,但通过更严格的参数检查,它在 2.1.5 中会中断。


    一个简单的解决方法是在块参数中设置一个默认参数,例如。

    klass.stub(:search) do |term, options|

    klass.stub(:search) do |term, options={}|

    您也可以在执行此操作后安全地删除 options ||= {} 行。

    【讨论】:

    • 可能不会在推理上是 100% 正确的,但是在块声明中使用默认参数应该可以解决您的错误。
    • 遇到同样的错误,但感谢您的回复 :)
    猜你喜欢
    • 2021-07-08
    • 2015-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多