No release in over 3 years
Low commit activity in last 3 years
A Gem that generate FactoryBot's Factory file from exsiting Hash, OpenStruct or Models.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 0.13.1
~> 3.0
> 5.0.0.1
>= 2.2.10
>= 0
>= 11.2.2

Runtime

>= 4.8.2
 Project Readme

Factory Bot Factory

Gem Version Gem Version

A Gem that helps you generate FactoryBot's Factory file from exsiting Hash, OpenStruct or ActiveModels.

factory_bot_factory_speed_demo

Installation

gem 'factory_bot_factory'

And then execute:

$ bundle

Or install it yourself as:

$ gem install factory_bot_factory

Quick Demo

require 'factory_bot_factory'

puts FactoryBotFactory.build({ id: 1 })
puts FactoryBotFactory.build(OpenStruct.new(id: 1))
puts FactoryBotFactory.build(User.last)
puts FactoryBotFactory.build(User.new)

More Options

  • nested_level - Build Nested Hash Factory (only support Hash and OpenStruct)
data = { id: 1, tags: ["tag1", "tag2"], address: { country: "US" }  }
puts FactoryBotFactory.build(data, nested_level: 2)

# output
FactoryBot.define do
  factory :hash, class: Hash do
    id { 1 }
    tags do
      [
        "tag1",
        "tag2"
      ]
    end
    address { build(:hash_address) }
    initialize_with { attributes }
  end

  factory :hash_address, class: Hash do
    country { "US" }
    initialize_with { attributes }
  end
end
  • file_path - Export output as file somewhere
FactoryBotFactory.build(data, file_path: "spec/factories/hash.rb")

require 'factory_bot'
FactoryBot.reload
FactoryBot.build(:hash, id: 2)
  • factory_name - Specify Factory Name
puts FactoryBotFactory.build({ id: 1 }, factory_name: 'order')

# output
FactoryBot.define do
  factory :order, class: Hash do
    id { 1 }
    initialize_with { attributes }
  end
end
  • klass - Specify Output Data Structure: Hash, OpenStruct and your ActiveModel or ActiveRecord Model

You can convert your Hash object to OpenStruct factory.

puts FactoryBotFactory.build({ id: 1 }, factory_name: 'order', klass: OpenStruct)

# output
FactoryBot.define do
  factory :order, class: OpenStruct do
    id { 1 }
    to_create {}
  end
end

Configuration

  • Default factory path

By configuring factory_path, it allows file auto-generation without specifying file_path.

FactoryBotFactory.configure do |config|
  config.factory_path = 'spec/factories'
end
  • Customize your own converter:

Converter should respond to call method and return single or array of executable values.

FactoryBotFactory.configure do |config|
  config.numeric_converter = Proc.new { |k, v| 'rand(99)' }
  config.string_converter = Proc.new do |k, v|
    if v.to_s.match?(/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/)
      'Random.uuid()'
    elsif k.to_s.include?('name')
      'Faker::Name.name'
    else
      "'#{v}'"
    end
  end
end

FactoryBotFactory.build({ name: 'My Name', id: "de9515ee-006e-4a28-8af3-e88a5c771b93", age: 10 })

# output
FactoryBot.define do
  factory :hash, class: Hash do
    name { Faker::Name.name }
    id { Random.uuid() }
    age { rand(99) }
    initialize_with { attributes }
  end
end

See more converters Here

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/cdragon1116/factory_bot_factory. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

The gem is available as open source under the terms of the MIT License.

Code of Conduct

Everyone interacting in the Factorybuilder project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.