{"id":15406486,"url":"https://github.com/odlp/factory_bot_variants","last_synced_at":"2025-07-11T15:10:56.027Z","repository":{"id":56332039,"uuid":"150721864","full_name":"odlp/factory_bot_variants","owner":"odlp","description":"Shorthand syntax for creating multiple variants with FactoryBot","archived":false,"fork":false,"pushed_at":"2023-03-17T03:51:37.000Z","size":20,"stargazers_count":1,"open_issues_count":2,"forks_count":0,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-04-06T12:13:25.699Z","etag":null,"topics":["factory-bot","factory-bot-rails"],"latest_commit_sha":null,"homepage":null,"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/odlp.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-09-28T10:05:58.000Z","updated_at":"2020-11-13T19:12:27.000Z","dependencies_parsed_at":"2024-10-19T12:31:08.384Z","dependency_job_id":"13e2e611-611b-4e5b-8ced-b6fafb685c16","html_url":"https://github.com/odlp/factory_bot_variants","commit_stats":{"total_commits":19,"total_committers":3,"mean_commits":6.333333333333333,"dds":"0.21052631578947367","last_synced_commit":"822c4d21a8afaef0d919eb4b954d1c187168d87c"},"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/odlp/factory_bot_variants","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/odlp%2Ffactory_bot_variants","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/odlp%2Ffactory_bot_variants/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/odlp%2Ffactory_bot_variants/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/odlp%2Ffactory_bot_variants/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/odlp","download_url":"https://codeload.github.com/odlp/factory_bot_variants/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/odlp%2Ffactory_bot_variants/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":264837724,"owners_count":23671082,"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":["factory-bot","factory-bot-rails"],"created_at":"2024-10-01T16:23:20.145Z","updated_at":"2025-07-11T15:10:55.935Z","avatar_url":"https://github.com/odlp.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# FactoryBotVariants\n\nIn tests it's common to create several objects which vary by only one or two\nattributes (particularly when asserting the order, or filtering, of entities):\n\n```ruby\nit \"sorts dogs oldest to youngest\" do\n  dog_1 = build(:dog, name: \"Fido\", age: 5)\n  dog_2 = build(:dog, name: \"Benji\", age: 10)\n  dog_3 = build(:dog, name: \"Spot\", age: 1)\n\n  expect(DogSorter.new.sort).to eq [dog_2, dog_1, dog_3]\nend\n```\n\nWith `FactoryBotVariants` you could write this as:\n\n```ruby\ndog_1, dog_2, dog_3 = build_variants(:dog, names: [\"Fido\", \"Benji\", \"Spot\"], ages: [5, 10, 1])\n```\n\nYou can specify attribute names in plural if you like; `names` will become\n`name` as the attributes are passed to your factory.\n\nIf you have common attributes to assign to each variant, use the `:all` key:\n\n```ruby\nbuild_variants(:dog, names: [\"Brutus\", \"Avril\"], all: { cute: false })\n```\n\n## Installation\n\nIn your gemfile:\n\n```ruby\ngroup :test do\n  gem \"factory_bot_variants\"\nend\n```\n\nThen `bundle install` and include the DSL where you'd like to use it:\n\n### In specific tests\n\n```ruby\nrequire \"factory_bot_variants/dsl\"\n\nRSpec.describe Dog do\n  include FactoryBotVariants::DSL\n\n  # Your spec\nend\n```\n\n### In all specs\n\n```ruby\nrequire \"factory_bot_variants/dsl\"\n\nRSpec.configure do |config|\n  config.include FactoryBotVariants::DSL\nend\n```\n\n## API\n\nThere's an equivalent `_variants` method per [FactoryBot strategy][strategies]:\n\n[strategies]: https://github.com/thoughtbot/factory_bot/blob/master/GETTING_STARTED.md#using-factories\n\n- `build_variants`\n- `create_variants`\n- `attributes_for_variants`\n- `build_stubbed_variants`\n\nArguments:\n\n1. Factory name\n1. Traits (optional)\n1. One or more variant attributes\n\nExample:\n\n```ruby\nbuild_variants(\n  factory_name,\n  trait,\n  attr1: [:a, :b],\n  attr2: [1, 2],\n  all: { common_attr: 1 },\n)\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fodlp%2Ffactory_bot_variants","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fodlp%2Ffactory_bot_variants","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fodlp%2Ffactory_bot_variants/lists"}