【问题标题】:Should eager loading be on or off in the test environment?在测试环境中应该打开还是关闭预加载?
【发布时间】:2015-03-11 17:54:24
【问题描述】:

这是在标准的 test.rb 中:

# Do not eager load code on boot. This avoids loading your whole application
# just for the purpose of running a single test. If you are using a tool that
# preloads Rails for running tests, you may have to set it to true.
config.eager_load = false

此建议是否适用于运行整个测试套件时,还是仅适用于运行单个测试时?

我如何决定是打开还是关闭?

在运行单个测试时设置为假,在运行整个套件时设置为真,有什么妙方吗?

(我怀疑我得到的某些类型的错误是关闭的结果)

【问题讨论】:

    标签: ruby-on-rails testing rspec


    【解决方案1】:

    渴望加载适用于每个测试,因此即使在您运行整个测试套件时也可以扩展。

    通常在 cmets 中提到的工具是测试工具(如 capybara),并且您几乎需要将 eager_loading 设置为 true,否则您将收到缺少常量的错误。

    如果您需要运行一些将eager_loading 设置为false 的测试以及将其他设置为true 的测试,您可以使用这样的方法:

    # config/environments/test.rb
    
      config.eager_load = !!(ENV['ENABLE_SPRING'] == 'true')
    
    

    然后你像这样运行特定批次的测试

    $ ENABLE_SPRING=true bundle exec rspec spec/test_name.rb
    

    来源:个人经历+https://guides.rubyonrails.org/autoloading_and_reloading_constants.html#autoloading-in-the-test-environment

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-19
      • 1970-01-01
      • 1970-01-01
      • 2011-11-20
      • 2022-12-01
      • 2022-08-18
      • 2013-09-07
      • 2021-08-30
      相关资源
      最近更新 更多