测试 RSpec 在 Rails 6 上有坑

SpiderEvgn · 2019年08月27日 · 最后由 jasl 回复于 2019年09月21日 · 6145 次阅读

我在 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' 解决了问题。

坑!

就是版本问题呗?换成最新版本就解决了

版本问题,适配 Rails 6 的还没正式发

SpiderEvgn 关闭了讨论。 03月05日 15:06
需要 登录 后方可回复, 如果你还没有账号请 注册新账号