57

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

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


注意:FactoryBot 以前被命名为 FactoryGirl

4

8 回答 8

165

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

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

于 2013-02-05T00:08:36.673 回答
17

--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

于 2012-07-28T22:32:39.130 回答
9

我有一个正好适合这个https://github.com/markburns/to_factory的宝石

于 2015-01-08T15:12:45.577 回答
4

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

@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
于 2018-05-13T18:12:09.263 回答
2

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

rails generate factory_girl:model users fname:string lname:string bdate:date email:string encrypted_pa​​ssword:string

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

检查这里这里

于 2013-05-17T19:55:27.030 回答
2

将 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

于 2019-10-31T13:35:59.397 回答
0

不完全相关。

我还构建了一个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

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

于 2021-09-19T10:08:18.737 回答
-1

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

于 2014-01-23T21:49:32.013 回答