动态添加所有引擎的工厂路径

发布于 2025-01-14 08:15:09 字数 440 浏览 3 评论 0原文

support/factory_bot.rb 中,我们添加了所有引擎的工厂路径,如下所示:

require 'factory_bot'
FactoryBot.definition_file_paths.unshift(
  MyEngine::One.factory_path, 
  MyEngine::Two.factory_path,
  SomethingElse.factory_path, 
)
FactoryBot.find_definitions

if defined? RSpec
  RSpec.configure do |config|
    config.include FactoryBot::Syntax::Methods
  end
end

有没有办法动态添加所有引擎的工厂路径?例如,如果我们要添加新引擎,则不需要对此文件进行任何调整。

In support/factory_bot.rb we add all the Engine's factory paths like this:

require 'factory_bot'
FactoryBot.definition_file_paths.unshift(
  MyEngine::One.factory_path, 
  MyEngine::Two.factory_path,
  SomethingElse.factory_path, 
)
FactoryBot.find_definitions

if defined? RSpec
  RSpec.configure do |config|
    config.include FactoryBot::Syntax::Methods
  end
end

Is there a way to dynamically add all my Engine's factory paths in? For example, if we were to add a new engine, we wouldn't need to make any adjustments to this file.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

别在捏我脸啦 2025-01-21 08:15:09

我能够使用以下命令动态添加所有工厂路径:

# frozen_string_literal: true
require 'factory_bot'

factory_paths = []

Rails::Engine.subclasses.each do |klass|
  engine = klass.name.chomp('::Engine')
  if engine.constantize.methods.include?(:factory_path)
    factory_paths << engine.constantize.factory_path
  end
end

FactoryBot.definition_file_paths.unshift(*factory_paths)

FactoryBot.find_definitions

if defined? RSpec
  RSpec.configure do |config|
    config.include FactoryBot::Syntax::Methods
  end
end

I was able to dynamically add all the factory paths with the following:

# frozen_string_literal: true
require 'factory_bot'

factory_paths = []

Rails::Engine.subclasses.each do |klass|
  engine = klass.name.chomp('::Engine')
  if engine.constantize.methods.include?(:factory_path)
    factory_paths << engine.constantize.factory_path
  end
end

FactoryBot.definition_file_paths.unshift(*factory_paths)

FactoryBot.find_definitions

if defined? RSpec
  RSpec.configure do |config|
    config.include FactoryBot::Syntax::Methods
  end
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文