简体   繁体   English

Rails 组织单元测试的方式是什么?

[英]What is the Rails way of organizing unit tests?

When creating unit tests in a Rails project, the first question I asked myself was "what is the convention for organizing unit tests" because nearly everything else is dictated by convention.在 Rails 项目中创建单元测试时,我问自己的第一个问题是“组织单元测试的约定是什么”,因为几乎所有其他内容都是由约定决定的。 Also, I'm curious if the convention changes between MiniTest, TestUnit and Rspec.另外,我很好奇 MiniTest、TestUnit 和 Rspec 之间的约定是否发生了变化。

What I have done so far is to mirror my Rails directory structure in my test/unit directory, like so:到目前为止,我所做的是在我的 test/unit 目录中镜像我的 Rails 目录结构,如下所示:

[Rails Root]
 /lib/
  ./postgres.rb
  ./awesome_parser.rb
 /app/helpers
  ./psychic_helper.rb

 /test/unit/
  ./lib/postgres.rb
  ./lib/awesome_parser.rb
  ./app/helpers/psychic_helper

What is the most conventional way of organizing tests, so that when others hop on this project they don't curse my name?组织测试的最传统方式是什么,这样当其他人加入这个项目时,他们就不会诅咒我的名字?

Rspec is like the above, except leaves out the 'app' dir, eg (from rails root): Rspec 与上面类似,除了省略了“app”目录,例如(来自rails root):

spec/controllers/user_controller_spec.rb
spec/models/user_spec.rb
spec/helpers/user_helpers_spec.rb

But the lib directory and others are still used:但是lib目录等还是用的:

spec/lib/user_stuff_spec.rb

Integration tests cross boundaries, so get their own dir:集成测试跨越边界,所以获取自己的目录:

spec/integration/route_specs.rb

Spec helpers have their own dir:规范助手有自己的目录:

spec/support/custom_matchers.rb

Probably not a convention, but we put our factories in their own dir:可能不是惯例,但我们将工厂放在自己的目录中:

spec/factories/user_factories.rb

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

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