【问题标题】:Prevent rake task dependency from loading Rails twice防止 rake 任务依赖加载 Rails 两次
【发布时间】:2014-02-20 17:12:31
【问题描述】:

遵循http://robots.thoughtbot.com/testing-your-factories-first 的示例 Rakefile 中的 task spec: :factory_specs 行告诉 rake 在“spec”任务之前执行“factory_specs”任务。

我的问题是rake spec 两次加载 Rails 环境,一次执行 factory_specs 任务,另一次执行 spec 任务。

如何防止我的 rake 命令加载 Rails 两次?

我的堆栈:

  • Ruby 1.9.3p484
  • Rails 3.2.13

【问题讨论】:

  • Rake 已经对此进行了优化。 (即,如果您正在调用两个任务,并且它们都具有相同的依赖关系,那么它只会被调用一次)。事实上,在一个调用链中,同一个任务永远不会被执行多次
  • 不适用于我的堆栈和场景。每次运行任务之前加载 Rails 大约需要 30 秒,而任务本身只需 2 秒即可运行。

标签: ruby-on-rails rake factory-bot rake-task


【解决方案1】:

使用最新版本的 FactoryGirl,您可以使用 FactoryGirl.lint 而不是在单独的 rake 任务中显式测试您的工厂。只需更新到最新版本并将其添加到您的spec_helper.rb

RSpec.configure do |config|
  config.before(:suite) do
    FactoryGirl.lint
  end
end

更多信息在Getting Started Guide

【讨论】:

  • 迄今为止最好的答案,谢谢! FactoryGirl 刚刚在 14 天前添加了此功能......恰逢其时。
猜你喜欢
  • 2022-08-15
  • 2017-10-04
  • 2012-01-22
  • 1970-01-01
  • 2016-03-23
  • 2012-05-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多