我一直在使用 rails 5.0.0.1,我是自动化测试的新手,并且使用 rspec-rails-3.5.2 来编写自动化。
我想测试一些基本的控制器渲染功能和实例变量分配。编写了一个控制器测试用例,它将检查是否呈现了 get_structure 模板并分配了所需的实例变量。
describe 'GET #get_structure' do
context 'get attributes to build tree structure' do
let(:plan) { FactoryGirl.create(:plan) }
it 'expects all keys to build structure' do
get :get_structure, params: { id: 6, school_subdomain: 'chrys' }
expect(response).to render_template(:get_structure)
expect(assigns.keys.include?('collection')).to be true
expect(assigns.keys.include?('structure')).to be true
expect(assigns.keys.include?('config')).to be true
end
end
end
Run Code Online (Sandbox Code Playgroud)
一旦我运行了测试用例,我意识到由于某些安全原因,assert_template 不再受支持。
NoMethodError: assert_template 已被提取到 gem 中。要继续使用它,请添加
gem 'rails-controller-testing'到您的 Gemfile。
由于它在 rspec-rails 文档中提到使用 rails-controller-testing gem 将添加回功能,我确实添加了它。
在 Rails 5.x 中,控制器测试已移至其自己的 gem 中,即 rails-controller-testing。在您的控制器规范中使用分配而不添加此 gem …