【问题标题】:Test failing in get :index with wrong number of arguments (given 2, expected 1) using Rspec使用 Rspec 使用错误数量的参数(给定 2,预期为 1)在 get :index 中测试失败
【发布时间】:2020-03-08 18:24:30
【问题描述】:

我有一个非常简单的 RSpec 控制器测试,如下所示:

require 'rails_helper'

RSpec.describe IndexController, type: :controller do
  describe 'GET #index' do
    it 'returns http success' do
      get :index
      expect(response).to have_http_status(:success)
    end
  end
end

它失败并出现错误:

  1) IndexController GET #index returns http success
     Failure/Error: get :index

     ActionView::Template::Error:
       wrong number of arguments (given 2, expected 1)
     # ./spec/controllers/index_controller_spec.rb:6:in `block (3 levels) in <top (required)>'
     # ------------------
     # --- Caused by: ---
     # ArgumentError:
     #   wrong number of arguments (given 2, expected 1)
     #   ./spec/controllers/index_controller_spec.rb:6:in `block (3 levels) in <top (required)>'

第 6 行是get :index。为什么它声称有 2 个论点?只给出了 1 个。

控制器如下所示:

class IndexController < ApplicationController
end

app/views/index/index.html.erb 中的视图如下所示:

Nothing to see here.

它可以在这条路线上正常工作:root to: 'index#index'

有什么想法吗?

【问题讨论】:

标签: ruby-on-rails testing rspec


【解决方案1】:

这是一个已知错误:https://github.com/rspec/rspec-rails/issues/2177

我通过升级到rails-rspec 4.0.0 来修复它,目前是测试版:

gem 'rspec-rails', '~> 4.0.0.beta4' # 4.0.0.beta4 due to https://github.com/rspec/rspec-rails/issues/2177

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-02
    • 2020-09-12
    • 2020-12-12
    • 1970-01-01
    • 2017-10-25
    • 1970-01-01
    相关资源
    最近更新 更多