rspec-rails 使用断言和赋值的控制器测试
rspec-rails controller testing with assertions and assigns
我一直在使用 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
一旦我 运行 我意识到的测试用例 assert_template 由于某些安全原因不再受支持。
NoMethodError:
assert_template has been extracted to a gem. To continue using it,
add gem 'rails-controller-testing'
to your Gemfile.
由于在 rspec-rails 文档中提到使用 rails-controller-testing gem 将重新添加功能,所以我确实添加了它。
In Rails 5.x, controller testing has been moved to its own gem which
is rails-controller-testing. Using assigns in your controller specs
without adding this gem will no longer work.
我添加了 rails-controller-testing-1.0.1 gem。同样在 rails-controller-testing gem 文档中提到
rspec-rails automatically integrates with this gem since version
3.5.0. Adding the gem to your Gemfile is sufficient.
但是添加之后还是报同样的错误
如何解决这个问题?还有其他方法可以进行控制器测试吗?
很长一段时间以来,我一直在努力解决这个问题。任何帮助,将不胜感激。提前致谢。
从下的release notesRails:支持Rails 5你可以找到推荐的测试控制器的方法是请求规格。
For new Rails apps: we don't recommend adding the
rails-controller-testing gem to your application. The official
recommendation of the Rails team and the RSpec core team is to write
request specs instead. Request specs allow you to focus on a single
controller action, but unlike controller tests involve the router, the
middleware stack, and both rack requests and responses. This adds
realism to the test that you are writing, and helps avoid many of the
issues that are common in controller specs. In Rails 5, request specs
are significantly faster than either request or controller specs were
in rails 4, thanks to the work by Eileen Uchitelle1 of the Rails
Committer Team.
请求测试也有点击率。因此,您无需使用 assert_template
.
进行测试
当有人用谷歌搜索 NoMethodError: assert_template has been extracted to a gem.
时,这个问题首先出现,这是当您尝试编写控制器规范或使用 render_template 或分配请求规范时出现的错误消息。
RSpec documentation still uses this as examples of what to do with both controller and request specs. The rationale of why to use request specs instead of controller specs is listed as an answer elsewhere by 。但我仍然对如何处理控制器或请求规格感到困惑。
答案好像不是。 assigns 和 assert_template have been deprecated,这意味着使用它们是一种代码味道。
与其编写检查模板是否正在呈现的测试,不如编写检查该模板将显示的内容或响应是否成功的测试。不要编写检查是否正在分配值的测试,而是检查数据库是否已更改,是否分配了 cookie 或会话,或者是否显示了值。
这对我来说是一个很难理解的概念,很多在线教程甚至 official rspec docs make reference to this,但简短的回答似乎是,不要这样做。我希望这有助于其他人搜索 assert_template 错误消息。
我一直在使用 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
一旦我 运行 我意识到的测试用例 assert_template 由于某些安全原因不再受支持。
NoMethodError: assert_template has been extracted to a gem. To continue using it, add
gem 'rails-controller-testing'
to your Gemfile.
由于在 rspec-rails 文档中提到使用 rails-controller-testing gem 将重新添加功能,所以我确实添加了它。
In Rails 5.x, controller testing has been moved to its own gem which is rails-controller-testing. Using assigns in your controller specs without adding this gem will no longer work.
我添加了 rails-controller-testing-1.0.1 gem。同样在 rails-controller-testing gem 文档中提到
rspec-rails automatically integrates with this gem since version 3.5.0. Adding the gem to your Gemfile is sufficient.
但是添加之后还是报同样的错误
如何解决这个问题?还有其他方法可以进行控制器测试吗? 很长一段时间以来,我一直在努力解决这个问题。任何帮助,将不胜感激。提前致谢。
从下的release notesRails:支持Rails 5你可以找到推荐的测试控制器的方法是请求规格。
For new Rails apps: we don't recommend adding the rails-controller-testing gem to your application. The official recommendation of the Rails team and the RSpec core team is to write request specs instead. Request specs allow you to focus on a single controller action, but unlike controller tests involve the router, the middleware stack, and both rack requests and responses. This adds realism to the test that you are writing, and helps avoid many of the issues that are common in controller specs. In Rails 5, request specs are significantly faster than either request or controller specs were in rails 4, thanks to the work by Eileen Uchitelle1 of the Rails Committer Team.
请求测试也有点击率。因此,您无需使用 assert_template
.
当有人用谷歌搜索 NoMethodError: assert_template has been extracted to a gem.
时,这个问题首先出现,这是当您尝试编写控制器规范或使用 render_template 或分配请求规范时出现的错误消息。
RSpec documentation still uses this as examples of what to do with both controller and request specs. The rationale of why to use request specs instead of controller specs is listed as an answer elsewhere by
答案好像不是。 assigns 和 assert_template have been deprecated,这意味着使用它们是一种代码味道。
与其编写检查模板是否正在呈现的测试,不如编写检查该模板将显示的内容或响应是否成功的测试。不要编写检查是否正在分配值的测试,而是检查数据库是否已更改,是否分配了 cookie 或会话,或者是否显示了值。
这对我来说是一个很难理解的概念,很多在线教程甚至 official rspec docs make reference to this,但简短的回答似乎是,不要这样做。我希望这有助于其他人搜索 assert_template 错误消息。