繁体   English   中英

FactoryBot 可以在创建模型后生成工厂吗?

[英]Can FactoryBot generate factories after your models have been created?

当在 Gemfile 的开发和测试块中包含 factory_bot_rails gem 时,rails 将在生成模型时自动生成工厂。

有没有办法在生成模型后生成工厂?


注意:FactoryBot 之前被命名为 FactoryGirl

首先,查看源项目以了解它是如何实现的:

https://github.com/thoughtbot/factory_bot_rails/blob/master/lib/generators/factory_bot/model/model_generator.rb

之后,尝试猜测它是如何工作的:

rails g factory_bot:model Car name speed:integer

结果是:

create  test/factories/cars.rb

以及内容:

# Read about factories at https://github.com/thoughtbot/factory_girl

FactoryBot.define do
   factory :car do
     name "MyString"
     speed 1
   end
end

请记住,当您使用 rails g 时,您可以随时撤消它,使用 rails d

rails d factory_bot:model Car name speed:integer

注意: FactoryBot之前被命名为FactoryGirl

--fixture-replacement选项将让您告诉 rails 为构建测试数据生成什么。 您可以在config/application.rb文件中将其设置为默认值,如下所示:

config.generators do |g|
  g.fixture_replacement :factory_bot, suffix_factory: 'factory'
end

来源: https ://github.com/thoughtbot/factory_bot_rails/blob/master/features/fixture_replacement_config.feature#L21

我有一个 gem 正是这个https://github.com/markburns/to_factory

这适用于我使用 rails g factory_bot:model 用户运行命令或只是将命令输出。 您仍然需要填写该值。

@run_command        = true
@force              = true
@columns_to_ignore  = %w[id created_at update_at]
@tables_to_ignore = %w[schema_migrations ar_internal_metadata]
tables = ActiveRecord::Base.connection.tables.reject{|t| (@tables_to_ignore || []).include?(t)}

tables.each do |table|
  klass = table.singularize.camelcase.constantize
    command = "rails g factory_bot:model #{klass.to_s} #{klass.columns.reject do |c|
      (@columns_to_ignore || []).include?(c.name)
    end.map do |d|
      "#{d.name}:#{d.sql_type == 'jsonb' ? 'json' : d.type}"
  end.join(' ')}"
  command << ' --force' if @force
  puts command
  puts %x{#{command}} if @run_command
  puts (1..200).to_a.map{}.join('-')
end

将 Factory Bot 配置为夹具替代品,这样您就不必手动创建工厂。

config/application.rb

config.generators do |g|
  g.test_framework :rspec, fixture: true
  g.fixture_replacement :factory_bot, dir: 'spec/factories'
end

更多信息: https ://github.com/thoughtbot/factory_bot_rails/blob/master/features/fixture_replacement_config.feature

这不是答案,但由于我还不能发表评论:我认为你可以用它来解决你的部分问题。 您可以使用名为 schema_to_scaffold 的 gem 生成 factory_girl:model 命令字符串。 它输出:

rails 生成 factory_girl:model users fname:string lname:string bdate:date email:string encrypted_password:string

来自您的 schema.rb 或您重命名的 schema.rb。

在这里这里检查

不完全相关。

我还构建了一个gem来从现有数据构建工厂。

希望它可以帮助您加快这个过程......

puts FactoryBotFactory.build(User.new, file_path: 'spec/factories/user.rb')
puts FactoryBotFactory.build(User.last, file_path: 'spec/factories/user.rb')

# example output from User.new
FactoryBot.define do
  factory :user, class: User do
    id { nil }
    name { nil }
    created_at { nil }
    updated_at { nil }
    display_name { nil }
    image_url { nil }
    is_active { true }
  end
end

如果您需要构建假数据,您还可以配置自定义转换器。

这里有一些很好的答案,但另一种选择是使用stepford 对于一些使用具有外键约束的模式的项目,deep_* 方法等可能会有所帮助,这是一种通过命令行生成工厂的简单方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM