【发布时间】:2019-10-27 16:37:12
【问题描述】:
我有一个全新的 rails 6 应用程序,并且我安装了 rspec。
我创建了一个控制器,当我运行 rspec 时出现此错误:
PagesController GET #index returns http success Failure/Error: get :index ActionView::Template::Error: wrong number of arguments (given 2, expected 1) # ./spec/controllers/pages_controller_spec.rb:7:in `block (3 levels) in <top (required)>' # ------------------ # --- Caused by: --- # ArgumentError: # wrong number of arguments (given 2, expected 1) # ./spec/controllers/pages_controller_spec.rb:7:in `block (3 levels) in <top (required)>'
生成的 rspec 测试如下所示:
require 'rails_helper'
RSpec.describe CartController, type: :controller do
describe "GET #index" do
it "returns http success" do
get :index
expect(response).to have_http_status(:success)
end
end
end
生成的代码中是否存在错误或其他环境问题导致此问题?
我看不出哪里有 2 个参数?
【问题讨论】:
-
不要将控制器规格添加到新应用中。 “请求规范提供了控制器规范的高级替代方案。事实上,从 RSpec 3.5 开始,Rails 和 RSpec 团队都不鼓励直接测试控制器,而是支持像请求规范这样的功能测试。”
标签: ruby-on-rails rspec