【问题标题】:How to discover why RSpec is taking so long to start?如何发现为什么 RSpec 需要这么长时间才能启动?
【发布时间】:2019-04-20 13:03:03
【问题描述】:

我知道有一种方法可以知道哪些规范花费的时间最多,但我怀疑 RSpec 的加载。规格本身不会花费太多时间,但负载却是。
有办法发现吗?
我正在处理 Rails 的遗留代码,我不知道哪些 gem 会影响它。

【问题讨论】:

标签: ruby-on-rails rspec rspec-rails rspec3


【解决方案1】:

对于任何来到这里的人来说,问题是每次我运行测试时 RSpec 都会被截断。
我在它启动时通过检查日志发现了它 (tail -f log/test.log)。
为了解决这个问题,我使用了database_cleaner gem 并将其配置为:transaction 作为清理策略。

config.before(:each) do
    DatabaseCleaner.strategy = :transaction
end

【讨论】:

  • 我这样做了,将您的 sn-p 添加到 spec_helper.rb,但表格不断截断 log/test.log 文件。我错过了什么吗?
【解决方案2】:

可以是特定于应用程序的(例如要加载的宝石数量)。为了显着加快 Rspec 加载时间,您可以使用 spring-commands-rspec 它为 Spring(Rails 应用程序预加载器)实现 rspec 命令,如下所示:

在您的 gemfile 中,添加:

gem 'spring-commands-rspec', group: :development

然后运行:bundle install,最后:

bundle exec spring binstub rspec

在此过程之前和之后运行 Rspec,您应该会看到巨大的改进。

更多信息在这里:

https://github.com/jonleighton/spring-commands-rspec https://schwad.github.io/ruby/rails/testing/2017/08/14/50-times-faster-rspec-loading.html https://www.netguru.com/blog/9-ways-to-speed-up-your-rspec-tests

【讨论】:

  • 最后运行 spring stop 可能有助于重启 Spring!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-08-22
  • 2023-02-25
  • 2019-11-13
  • 1970-01-01
  • 1970-01-01
  • 2022-01-15
相关资源
最近更新 更多