我在 Rails6 上用 rspec 测试一个基础的 controller action 遇到如下错误:
Failures:
1) HomeController GET index render index
Failure/Error: get :index
ActionView::Template::Error:
wrong number of arguments (given 2, expected 1)
# ./spec/controllers/home_controller_spec.rb:8:in `block (3 levels) in <top (required)>'
# ------------------
# --- Caused by: ---
# ArgumentError:
# wrong number of arguments (given 2, expected 1)
# ./spec/controllers/home_controller_spec.rb:8:in `block (3 levels) in <top (required)>'
Finished in 0.04047 seconds (files took 7.62 seconds to load)
1 example, 1 failure
代码如下:
# app/controllers/home_controller.rb
class HomeController < ApplicationController
def index
end
end
# config/routes.rb
root to: 'home#index'
# spec/controllers/home_controller_spec.rb
require 'rails_helper'
RSpec.describe HomeController, type: :controller do
describe "GET index" do
it "response successfully" do
get :index
expect(response.status).to eq(200)
end
end
end
废了半天劲最后找到这篇文章:https://cloud.tencent.com/developer/ask/211674
然后把 Gemfile 改成 gem 'rspec-rails', github: 'rspec/rspec-rails', branch: '4-0-dev'
解决了问题。
坑!