spec/rails_helper.rb 与 spec/spec_helper.rb 有何不同?我需要吗? [英] How is spec/rails_helper.rb different from spec/spec_helper.rb? Do I need it?

查看:44
本文介绍了spec/rails_helper.rb 与 spec/spec_helper.rb 有何不同?我需要吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是第二次做 Rails 教程.当我输入这个

I am doing the Rails Tutorial for the second time. When I enter this

rails generate integration_test static_pages

我得到 spec/rails_helper.rbspec/spec_helper.rb 而不是 spec/spec_helper.rb

I get spec/rails_helper.rb and spec/spec_helper.rb instead of just spec/spec_helper.rb

现在,当我运行我的测试时,它们比我上次运行时更长(更详细")并且更慢.我想知道这两个文件之间的区别是什么,以及我是否做错了什么.另外,有没有办法摆脱 rails_helper.rb 文件而不会弄乱一切?

Now when I run my tests, they are longer (more "verbose") and slower than when I did this last time. I am wondering what the difference between the two files is, and if I did something wrong. Also, is there a way to get rid of the rails_helper.rb file without messing everything up?

推荐答案

rspec-rails 3 生成 spec_helper.rbrails_helper.rb.spec_helper.rb 用于不依赖于 Rails 的规范(例如 lib 目录中的类的规范).rails_helper.rb 用于依赖于 Rails 的规范(在 Rails 项目中,大部分或全部).rails_helper.rb 需要 spec_helper.rb.所以不,不要去掉 rails_helper.rb;在您的规范中需要它(而不是 spec_helper.rb).

rspec-rails 3 generates spec_helper.rb and rails_helper.rb. spec_helper.rb is for specs which don't depend on Rails (such as specs for classes in the lib directory). rails_helper.rb is for specs which do depend on Rails (in a Rails project, most or all of them). rails_helper.rb requires spec_helper.rb. So no, don't get rid of rails_helper.rb; require it (and not spec_helper.rb) in your specs.

如果你希望你的非 Rails 依赖规范强制它们不依赖 Rails,并且在你自己运行它们时尽可能快地运行,你可以需要 spec_helper.rb 而不是 rails_helper.rb .但是在您的 .rspec 中使用 -r rails_helper 非常方便,而不需要在每个规范文件中使用一个或另一个助手,因此这肯定是一种流行的方法.

If you want your non-Rails-dependent specs to enforce that they're non-Rails-dependent, and to run as fast as possible when you run them by themselves, you could require spec_helper.rb rather than rails_helper.rb in those. But it's very convenient to -r rails_helper in your .rspec rather than requiring one helper or the other in each spec file, so that is sure to be a popular approach.

如果您使用的是 spring 预加载器,则每个类只需要加载一次,并且 spring 急切地加载类,即使你只运行一个需要 spec_helper 的规范,所以没有在某些文件中只需要 spec_helper 的价值.

If you're using the spring preloader, each class only needs to be loaded once, and spring loads classes eagerly even if you only run a single spec that requires spec_helper, so there isn't as much value in requiring only spec_helper in some files.

来源:https://www.relishapp.com/rspec/rspec-rails/docs/upgrade#default-helper-files

这篇关于spec/rails_helper.rb 与 spec/spec_helper.rb 有何不同?我需要吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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