简体   繁体   English

控制器规格与请求规格?

[英]Controller Specs vs Request Specs?

I'm working on a rails API and I'm now planning on writing some RSpec tests for the controllers. 我正在研究rails API,我现在正计划为控制器编写一些RSpec测试。 I've been reading around and I haven't been able to figure out what the actual difference between controller specs and request specs are and which one I should probably use if I'm testing an API. 我一直在阅读,我无法弄清楚控制器规格和请求规格之间的实际区别是什么,以及如果我正在测试API,我应该使用哪一个。

Indeed, the RSpec team officially states controller specs are now obsolete . 实际上,RSpec团队正式声明控制器规格现已过时

http://rspec.info/blog/2016/07/rspec-3-5-has-been-released/ http://rspec.info/blog/2016/07/rspec-3-5-has-been-released/

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提交者团队的Eileen Uchitelle1的工作,请求规范明显快于rails 4中的请求或控制器规范。

Rails 3 & 4 Rails 3和4

Controller specs - A controller spec is an RSpec wrapper for a Rails functional test. 控制器规范 - 控制器规范是Rails功能测试的RSpec包装器。 It allows you to simulate a single http request in each example, and then specify expected outcomes 它允许您在每个示例中模拟单个http请求,然后指定预期结果

Request specs - Request specs provide a thin wrapper around Rails' integration tests, and are designed to drive behavior through the full stack, including routing (provided by Rails) and without stubbing (that's up to you). 请求规范 - 请求规范提供了一个围绕Rails集成测试的瘦包装器,旨在通过完整堆栈驱动行为,包括路由(由Rails提供)和没有存根(由您决定)。

So if you want to test API controllers I would recommend to use Controller specs as you are testing single requests. 因此,如果您想测试API控制器,我建议您在测试单个请求时使用Controller specs


Rails 5+ Rails 5+

Rails 5 improved the speed and realism of request specs over Rails version 4's controller and request specs. Rails 5提高了Rails版本4控制器和请求规范的请求规范的速度和真实性。 The official recommendation of the Rails team and the RSpec core team is to write request specs instead (of controller specs) . Rails团队和RSpec核心团队的官方建议是编写请求规范(控制器规范)

Quoting Aaron Sumner: 引用亚伦萨姆纳:

Both the Rails and RSpec teams suggest replacing or removing your app's controller tests (also known as the functional test layer), in favor of directly testing models (units), or with higher-level integration tests. Rails和RSpec团队都建议更换或删除应用程序的控制器测试(也称为功能测试层),支持直接测试模型(单元)或更高级别的集成测试。

For a new API, I prefer to use request spec and hit my "end points" (aka Models and Business logic) than using controller tests. 对于一个新的API,我更喜欢使用请求规范并点击我的“终点”(又称模型和业务逻辑),而不是使用控制器测试。

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

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