简体   繁体   English

带有断言和分配的 rspec-rails 控制器测试

[英]rspec-rails controller testing with assertions and assigns

I have been using rails 5.0.0.1, am new to automation testing and using rspec-rails-3.5.2 for writing automation.我一直在使用 rails 5.0.0.1,我是自动化测试的新手,并且使用 rspec-rails-3.5.2 来编写自动化。

I want to test some basic controller rendering functionality and instance variable assignments.我想测试一些基本的控制器渲染功能和实例变量分配。 Wrote a controller test case which will check whether get_structure template is rendered and required instance variables are assigned.编写了一个控制器测试用例,它将检查是否呈现了 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

Once i ran the test cases i realized assert_template is no longer supported due to some security reasons.一旦我运行了测试用例,我意识到由于某些安全原因,assert_template 不再受支持。

NoMethodError: assert_template has been extracted to a gem. NoMethodError: assert_template 已被提取到 gem 中。 To continue using it, add gem 'rails-controller-testing' to your Gemfile.要继续使用它,请将gem 'rails-controller-testing'到您的 Gemfile。

Since its mentioned in rspec-rails documentation that using rails-controller-testing gem will add back the functionality, i did add it.由于它在 rspec-rails 文档中提到使用 rails-controller-testing gem 将添加回功能,我确实添加了它。

In Rails 5.x, controller testing has been moved to its own gem which is rails-controller-testing.在 Rails 5.x 中,控制器测试已移至其自己的 gem 中,即 rails-controller-testing。 Using assigns in your controller specs without adding this gem will no longer work.在您的控制器规范中使用分配而不添加此 gem 将不再有效。

I have added rails-controller-testing-1.0.1 gem.我添加了 rails-controller-testing-1.0.1 gem。 Also in the rails-controller-testing gem documentation it is mentioned that同样在 rails-controller-testing gem 文档中提到

rspec-rails automatically integrates with this gem since version 3.5.0. rspec-rails 自 3.5.0 版起自动与此 gem 集成。 Adding the gem to your Gemfile is sufficient.将 gem 添加到您的 Gemfile 中就足够了。

But even after adding, it still throws the same error.但即使添加后,它仍然抛出相同的错误。

How to resolve this issue?如何解决这个问题? Is there any other way of doing controller tests.有没有其他方法可以进行控制器测试。 I have been banging my head over this for quite sometime.一段时间以来,我一直在为此烦恼。 Any help would be appreciated.任何帮助,将不胜感激。 Thanks in advance.提前致谢。

From the release notes under Rails: Support for Rails 5 you can find the recommended way to test controllers is Request specs .Rails: Support for Rails 5下的发行说明中,您可以找到测试控制器的推荐方法是Request specs

For new Rails apps: we don't recommend adding the rails-controller-testing gem to your application.对于新的 Rails 应用程序:我们不建议将 rails-controller-testing gem 添加到您的应用程序中。 The official recommendation of the Rails team and the RSpec core team is to write request specs instead. Rails 团队和 RSpec 核心团队的官方建议是改为编写请求规范 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.在 Rails 5 中,请求规范比 Rails 4 中的请求规范或控制器规范要快得多,这要归功于 Rails 提交团队的 Eileen Uchitelle1 所做的工作。

Request tests hits views also.请求测试也会命中视图。 So there you no need to test with assert_template .因此,您无需使用assert_template进行测试。

This question comes up first when one googles for NoMethodError: assert_template has been extracted to a gem.NoMethodError: assert_template has been extracted to a gem.搜索NoMethodError: assert_template has been extracted to a gem.时,这个问题首先出现NoMethodError: assert_template has been extracted to a gem. which is the error message that comes up when you try to write controller specs or request specs with either render_template or assigns.这是当您尝试使用 render_template 或assigns 编写控制器规范或请求规范时出现的错误消息。

The RSpec documentation still uses this as examples of what to do with both controller and request specs. RSpec 文档仍然使用它作为如何处理控制器和请求规范的示例。 The rationale of why to use request specs instead of controller specs is listed as an answer elsewhere by Narashima Reddy . Narashima Reddy在别处列出了为什么使用请求规范而不是控制器规范的理由 But I was still puzzled what to do with either controller or request specs.但我仍然不知道如何处理控制器或请求规范。

The answer seems to be not to.答案似乎是否定的。 Both assigns and assert_template have been deprecated , which means use of them is a code smell. assigns 和 assert_template都已弃用,这意味着使用它们是一种代码味道。

Instead of writing a test that checks for whether a template is being rendered, write a test that checks for what will be displayed by that template, or that the response is successful.与其编写一个测试来检查是否正在呈现模板,不如编写一个测试来检查该模板将显示什么,或者响应是否成功。 Instead of writing a test that checks that a value is being assigned, check to see whether the database was changed, or a cookie or session were assigned, or a value is displayed.与其编写检查是否已分配值的测试,不如检查数据库是否已更改,是否分配了 cookie 或会话,或是否显示了值。

This has been a difficult concept for me to grasp, as many online tutorials and even the official rspec docs make reference to this , but the short answer seems to be, don't do it.这对我来说是一个难以理解的概念,因为许多在线教程甚至官方 rspec 文档都参考了 this ,但简短的回答似乎是,不要这样做。 I hope this helps someone else searching for the assert_template error message.我希望这有助于其他人搜索 assert_template 错误消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM