{"id":13880157,"url":"https://github.com/fnando/factory_bot-preload","last_synced_at":"2025-04-04T18:10:03.842Z","repository":{"id":43058079,"uuid":"1597534","full_name":"fnando/factory_bot-preload","owner":"fnando","description":"Preload factories (factory_bot) just like fixtures. It will be easy and, probably, faster!","archived":false,"fork":false,"pushed_at":"2023-09-05T11:58:13.000Z","size":108,"stargazers_count":134,"open_issues_count":4,"forks_count":27,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-28T17:09:05.060Z","etag":null,"topics":["factories","factory-bot","factory-girl","minitest","preload-factories","rspec"],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/fnando.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":".github/FUNDING.yml","license":"LICENSE.md","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":".github/CODEOWNERS","security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":["fnando"],"custom":["https://paypal.me/nandovieira/🍕"]}},"created_at":"2011-04-11T05:21:47.000Z","updated_at":"2025-03-08T23:08:16.000Z","dependencies_parsed_at":"2024-06-21T13:00:53.222Z","dependency_job_id":"9a0a0e5a-5b03-4c8d-b738-4cfe754a1b23","html_url":"https://github.com/fnando/factory_bot-preload","commit_stats":{"total_commits":84,"total_committers":13,"mean_commits":6.461538461538462,"dds":"0.45238095238095233","last_synced_commit":"e94a0ef8e2ffacff5de753efdc0a3b6f8f1a3acb"},"previous_names":[],"tags_count":20,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fnando%2Ffactory_bot-preload","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fnando%2Ffactory_bot-preload/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fnando%2Ffactory_bot-preload/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fnando%2Ffactory_bot-preload/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/fnando","download_url":"https://codeload.github.com/fnando/factory_bot-preload/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247226215,"owners_count":20904465,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["factories","factory-bot","factory-girl","minitest","preload-factories","rspec"],"created_at":"2024-08-06T08:02:49.425Z","updated_at":"2025-04-04T18:10:03.799Z","avatar_url":"https://github.com/fnando.png","language":"Ruby","readme":"# factory_bot-preload\n\n[![ruby-tests](https://github.com/fnando/factory_bot-preload/actions/workflows/ruby-tests.yml/badge.svg)](https://github.com/fnando/factory_bot-preload/actions/workflows/ruby-tests.yml)\n[![Gem](https://img.shields.io/gem/v/factory_bot-preload.svg)](https://rubygems.org/gems/factory_bot-preload)\n[![Gem](https://img.shields.io/gem/dt/factory_bot-preload.svg)](https://rubygems.org/gems/factory_bot-preload)\n\nWe all love Rails fixtures because they're fast, but we hate to deal with\nYAML/CSV/SQL files. Here enters\n[factory_bot](https://rubygems.org/gems/factory_bot) (FB).\n\nNow, you can easily create records by using predefined factories. The problem is\nthat hitting the database everytime to create records is pretty slow. And\nbelieve me, you'll feel the pain when you have lots of tests/specs.\n\nSo here enters Factory Bot Preload (FBP). You can define which factories will be\npreloaded, so you don't have to recreate it every time (that will work for\n99.37% of the time, according to statistics I just made up).\n\n## Installation\n\n    gem install factory_bot-preload\n\n## Intructions\n\n### Setup\n\nAdd both FB and FBP to your Gemfile:\n\n```ruby\nsource \"https://rubygems.org\"\n\ngem \"rails\"\ngem \"pg\"\n\ngroup :test, :development do\n  gem \"factory_bot\"\n  gem \"factory_bot-preload\", require: false\nend\n```\n\nNotice that adding `require: false` is important; otherwise you won't be able to\nrun commands such as `rails db:test:prepare`.\n\n### RSpec Setup\n\nOn your `spec/spec_helper.rb` file, make sure that transactional fixtures are\nenabled. Here's is my file without all those RSpec comments:\n\n```ruby\nENV[\"RAILS_ENV\"] ||= \"test\"\nrequire File.expand_path(\"../../config/environment\", __FILE__)\nrequire \"rspec/rails\"\n\n# First, load factory_bot/preload.\nrequire \"factory_bot/preload\"\n\n# Then load your factories\nDir[Rails.root.join(\"spec/support/factories/**/*.rb\")].each do |file|\n  require file\nend\n\nRSpec.configure do |config|\n  config.use_transactional_fixtures = true\n  config.mock_with :rspec\nend\n```\n\nYou may want to configure the generated helper names. For instance, imagine you\nhave a namespace like `MyApp::Models::User`. That'd generate a helper method\nlike `myapp_models_user`. If you don't have conflicting names, you can strip\n`myapp_models_` like this:\n\n```ruby\nFactoryBot::Preload.helper_name = lambda do |class_name, helper_name|\n  helper_name.gsub(/^myapp_models_/, \"\")\nend\n```\n\n### Minitest Setup\n\nOn your `test/test_helper.rb` file, make sure that transaction fixtures are\nenabled. Here's what your file may look like:\n\n```ruby\nENV[\"RAILS_ENV\"] ||= \"test\"\nrequire_relative \"../config/environment\"\nrequire \"rails/test_help\"\n\nmodule ActiveSupport\n  class TestCase\n    self.use_instantiated_fixtures = true\n  end\nend\n\n# First, load factory_bot/preload.\nrequire \"factory_bot/preload\"\n\n# Then load your factories.\nDir[\"./test/support/factories/**/*.rb\"].each do |file|\n  require file\nend\n\n# Finally, setup minitest.\n# Your factories won't behave correctly unless you\n# call `FactoryBot::Preload.minitest` after loading them.\nFactoryBot::Preload.minitest\n```\n\n### Usage\n\nCreate your factories and load it from your setup file (either\n`test/test_helper.rb` or `spec/spec_helper.rb`) You may have something like\nthis:\n\n```ruby\nFactoryBot.define do\n  factory :user do\n    name \"John Doe\"\n    sequence(:email) {|n| \"john#{n}@example.org\" }\n    sequence(:username) {|n| \"john#{n}\" }\n    password \"test\"\n    password_confirmation \"test\"\n  end\n\n  factory :projects do\n    name \"My Project\"\n    association :user\n  end\nend\n```\n\nTo define your preloadable factories, just use the `preload` method:\n\n```ruby\nFactoryBot.define do\n  factory :user do\n    name \"John Doe\"\n    sequence(:email) {|n| \"john#{n}@example.org\" }\n    sequence(:username) {|n| \"john#{n}\" }\n    password \"test\"\n    password_confirmation \"test\"\n  end\n\n  factory :projects do\n    name \"My Project\"\n    association :user\n  end\n\n  preload do\n    factory(:john) { create(:user) }\n    factory(:myapp) { create(:project, user: users(:john)) }\n  end\nend\n```\n\nYou can also use preloaded factories on factory definitions.\n\n```ruby\nFactoryBot.define do\n  factory :user do\n    # ...\n  end\n\n  factory :projects do\n    name \"My Project\"\n    user { users(:john) }\n  end\n\n  preload do\n    factory(:john) { create(:user) }\n    factory(:myapp) { create(:project, user: users(:john)) }\n  end\nend\n```\n\nLike Rails fixtures, FBP will define methods for each model. You can use it on\nyour examples and alike.\n\n```ruby\nrequire \"test_helper\"\n\nclass UserTest \u003c ActiveSupport::TestCase\n  test \"returns john's record\" do\n    assert_instance_of User, users(:john)\n  end\n\n  test \"returns myapp's record\" do\n    assert_equal users(:john), projects(:myapp).user\n  end\nend\n```\n\nOr if you're using RSpec:\n\n```ruby\nrequire \"spec_helper\"\n\ndescribe User do\n  let(:user) { users(:john) }\n\n  it \"returns john's record\" do\n    users(:john).should be_an(User)\n  end\n\n  it \"returns myapp's record\" do\n    projects(:myapp).user.should == users(:john)\n  end\nend\n```\n\nThat's it!\n\n## Maintainer\n\n- [Nando Vieira](https://github.com/fnando)\n\n## Contributors\n\n- https://github.com/fnando/factory_bot-preload/contributors\n\n## Contributing\n\nFor more details about how to contribute, please read\nhttps://github.com/fnando/factory_bot-preload/blob/main/CONTRIBUTING.md.\n\n## License\n\nThe gem is available as open source under the terms of the\n[MIT License](https://opensource.org/licenses/MIT). A copy of the license can be\nfound at https://github.com/fnando/factory_bot-preload/blob/main/LICENSE.md.\n\n## Code of Conduct\n\nEveryone interacting in the factory_bot-preload project's codebases, issue\ntrackers, chat rooms and mailing lists is expected to follow the\n[code of conduct](https://github.com/fnando/factory_bot-preload/blob/main/CODE_OF_CONDUCT.md).\n","funding_links":["https://github.com/sponsors/fnando","https://paypal.me/nandovieira/🍕"],"categories":["Ruby"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffnando%2Ffactory_bot-preload","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffnando%2Ffactory_bot-preload","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffnando%2Ffactory_bot-preload/lists"}