首页 > 解决方案 > rspec rails 6 控制器

问题描述

我使用 rails 6 创建了一个新项目,但无法使用 rspec 3.8 或 3.9.0.pre 测试许多控制器,例如这个测试:

it 'OK' do
  get :index
  expect(response).to be_ok
end

提高

Failure/Error: render template: 'rig_masters/index'

     ActionView::Template::Error:
       wrong number of arguments (given 2, expected 1)

如果我有一个呈现json的控制器,它会通过,例如,如果控制器是

def index
  @components = Component.recent
  render json: @components
end

测试通过

但是如果我尝试渲染一个像

def index
  @components = Component.recent
end

甚至

def index
  @components = Component.recent
  render template: 'components/index'
end

引发ActionView::Template::Error: wrong number of arguments (given 2, expected 1)错误

任何帮助使这些测试通过将不胜感激。

标签: ruby-on-railsrspec-railsruby-on-rails-6

解决方案


RSpec 还不兼容 Rails 6。但是,您可以使用 rspec-rails 4.0.0.beta2,它将解决此特定问题!

链接:https ://github.com/rspec/rspec-rails/issues/2155


推荐阅读