测试 RSpec 在 Rails 6 上有坑

SpiderEvgn · August 27, 2019 · Last by jasl replied at September 21, 2019 · 6133 hits

我在 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 closed this topic. 05 Mar 15:06
You need to Sign in before reply, if you don't have an account, please Sign up first.