文件和目录结构的 RSpec 命名约定 [英] RSpec naming conventions for files and directory structure

查看:47
本文介绍了文件和目录结构的 RSpec 命名约定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写 RSpec 测试,但我已经到了无法在不同网站上阅读相同意见的地步.当我们处理 spec/controllers 和 spec/models 目录时,RSpec 的目录结构很清楚,但我得到的冲突信息处理的是测试视图.

I'm writing RSpec tests and I have come to a point where I am not reading the same opinions on different websites. The directory structure for RSpec is clear when we are dealing with spec/controllers and spec/models directories, but the conflicting information I am getting deals with testing views.

我想将这些测试放在 spec/integration/目录中,但我读到它应该在 spec/integration 下,但另一本书说是 spec/requests.这有关系吗?

I would like to just put these tests in a spec/integration/ directory, but I have read that it's supposed to be under spec/integration, but another book says spec/requests. Does this matter?

为了使情况进一步复杂化,我阅读了有关命名测试实际文件名的相互矛盾的信息!例如,如果我有一个名为people"的控制器/模型/视图目录(我使用 haml),我应该像这样命名文件:

To further complicate the situation, I have read conflicting information on naming the actual file names of the tests! For example, if I had a controller/model/view directory called 'people' (I i use haml), I should name the files like this:

spec/integration/people.html.haml_spec.rb

spec/integration/people.html.haml_spec.rb

然而,另一本书提出了这一点:

However, another book suggests this:

spec/requests/people_spec.rb

spec/requests/people_spec.rb

我想稍微解释一下 RSpec 中的命名约定,为什么我应该命名视图测试的特定名称,以及将它们放在规范/集成或规范/请求下是否重要.我觉得我在这里随风而去.

I would like a little explanation of naming conventions within RSpec and WHY I should name the view tests specific names, as well as whether or not it matters to put them under spec/integration or spec/requests. I feel like I'm going with the wind here.

非常感谢任何帮助.

推荐答案

检查书籍的日期和编写它们的 RSpec 的版本.随着时间的推移,命名结构略有变化.

Check the dates of the books and the versions of RSpec for which they were written. The naming structure has changed slightly over time.

根据 rspec-rails 的文档,请求规范可以在 spec/requests 中,规范/api,或规范/集成.我更喜欢将请求规范放在规范/请求中.

According to the docs for rspec-rails, request specs can go in spec/requests, spec/api, or spec/integration. I prefer to put request specs in spec/requests.

为了让事情更有趣,如果您使用 Capybararspec-rails,它将适用于 Capybara 1.x 的规范/请求,以及 Capybara 2 的规范/功能.

To make things more interesting, if you are using Capybara with rspec-rails, it will work with spec/requests for Capybara 1.x, and spec/features for Capybara 2.

对于单独的spec文件名,当有一个特定的类被测试时,比如Rails模型,你应该使用类似的spec文件名:

As to individual spec file names, when there is a specific class under test, like a Rails model, you should use an analogous spec file name:

app/models/user.rb -> spec/models/user_spec.rb

查看规范应使用模板名称:

View specs should use the template name:

app/views/users/index.html.erb -> spec/views/users/index.html.erb_spec.rb

命名空间模型应该在规范文件路径中包含命名空间:

Namespaced models should include the namespace in the spec file path:

app/models/admin/user.rb -> spec/models/admin/user_spec.rb

RSpec 脚手架生成器是展示这些规范所属位置的很好指南.

The RSpec scaffold generator is a good guide for showing where these specs belong.

当没有特定的类被测试时,就像请求规范的情况一样,恕我直言,你应该随意使用一个描述被测试事物的名称.例如.spec/requests/place_an_order_spec.rb.

When there is no specific class under test, as is the case with request specs, IMHO you should feel free to use a name that describes the thing being tested. E.g. spec/requests/place_an_order_spec.rb.

这篇关于文件和目录结构的 RSpec 命名约定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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