控制器规格与请求规格? [英] Controller Specs vs Request Specs?

查看:38
本文介绍了控制器规格与请求规格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个 rails API,我现在正计划为控制器编写一些 RSpec 测试.我一直在阅读,但一直无法弄清楚控制器规范和请求规范之间的实际区别是什么,以及如果我正在测试 API,我可能应该使用哪一个.

I'm working on a rails API and I'm now planning on writing some RSpec tests for the controllers. 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.

推荐答案

Rails 3 &4

控制器规范 - 控制器规范是 Rails 功能测试的 RSpec 包装器.它允许您在每个示例中模拟单个 http 请求,然后指定预期结果

Rails 3 & 4

Controller specs - A controller spec is an RSpec wrapper for a Rails functional test. It allows you to simulate a single http request in each example, and then specify expected outcomes

请求规范 - 请求规范提供了一个围绕 Rails 集成测试的薄包装,并且是旨在通过整个堆栈驱动行为,包括路由(由 Rails 提供)并且没有存根(这取决于您).

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).

因此,如果您想测试 API 控制器,我建议您在测试单个请求时使用 Controller specs.

So if you want to test API controllers I would recommend to use Controller specs as you are testing single requests.

Rails 5 比 Rails 版本 4 的控制器和请求规范提高了请求规范的速度和真实性.Rails 团队和 RSpec 核心团队的官方推荐是编写请求规范而不是(而不是控制器规范).

Rails 5 improved the speed and realism of request specs over Rails version 4's controller and request specs. The official recommendation of the Rails team and the RSpec core team is to write request specs instead (of controller specs).

这篇关于控制器规格与请求规格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆