Ruby-on-rails FactoryBot 能否在您的模型创建后生成工厂?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11702265/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can FactoryBot generate factories after your models have been created?
提问by GuiDoody
When including the factory_bot_rails gem in your dev and test blocks in Gemfile, rails will generate factories automatically when your models are generated.
当在 Gemfile 中的开发和测试块中包含 factory_bot_rails gem 时,rails 将在生成模型时自动生成工厂。
Is there a way to generate factories after your models have been generated?
有没有办法在你的模型生成后生成工厂?
Eduardo Santana's ANSWER SHOULD BE CORRECT
Note: FactoryBot was previously named FactoryGirl
注意:FactoryBot 以前被命名为 FactoryGirl
采纳答案by cjhveal
The --fixture-replacementoption will let you tell rails what to generate for building test data. You can set this as a default in your config/application.rbfile, like so:
该--fixture-replacement选项将让您告诉 rails 为构建测试数据生成什么。您可以将其设置为config/application.rb文件中的默认值,如下所示:
config.generators do |g|
g.fixture_replacement :factory_girl
end
回答by Eduardo Santana
First thing, look at the source project to find out how it was implemented:
首先,查看源项目以了解它是如何实现的:
After that, try to guess how it works:
之后,尝试猜测它是如何工作的:
rails g factory_bot:model Car name speed:integer
The result is:
结果是:
create test/factories/cars.rb
And the content:
和内容:
# Read about factories at https://github.com/thoughtbot/factory_girl
FactoryBot.define do
factory :car do
name "MyString"
speed 1
end
end
Remember, when you use rails g, you can always undo it, with rails d
请记住,当您使用 rails g 时,您可以随时撤消它,使用 rails d
rails d factory_bot:model Car name speed:integer
Note: FactoryBot was previously named FactoryGirl
注意:FactoryBot 以前被命名为 FactoryGirl
回答by Mark Burns
I have a gem for exactly this https://github.com/markburns/to_factory
我有一个关于这个https://github.com/markburns/to_factory的宝石
回答by frenesim
This is not an answer, but since I cannot comment yet: I think you can use this to solve part of your problem. You can use a gem called schema_to_scaffold to generate a factory_girl:model command string. It outputs:
这不是答案,但由于我还不能发表评论:我认为您可以使用它来解决部分问题。您可以使用名为 schema_to_scaffold 的 gem 来生成 factory_girl:model 命令字符串。它输出:
rails generate factory_girl:model users fname:string lname:string bdate:date email:string encrypted_password:string
rails 生成 factory_girl:model users fname:string lname:string bdate:date email:string encrypted_password:string
from your schema.rb or your renamed schema.rb.
来自您的 schema.rb 或您重命名的 schema.rb。
回答by Davinj
This works for me using rails g factory_bot:model User either running the command or just puts'ing the command out. You do still have to fill in the value.
这适用于我使用 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
回答by Ruto Collins
Configure Factory Bot as the fixture replacement so you do not have to create factories manually.
将 Factory Bot 配置为夹具替代品,这样您就不必手动创建工厂。
In config/application.rb:
在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
回答by Gary S. Weaver
Some good answers here, but another option is to use stepford. For some projects that use schemas that have foreign key constraints, the deep_* methods, etc. might help, and it is a simple way to generate factories via command-line.
这里有一些很好的答案,但另一种选择是使用stepford。对于一些使用具有外键约束的模式的项目,deep_* 方法等可能会有所帮助,这是一种通过命令行生成工厂的简单方法。

