Skip to content

Shorthand syntax for creating multiple variants with FactoryBot

License

Notifications You must be signed in to change notification settings

odlp/factory_bot_variants

Repository files navigation

FactoryBotVariants

In tests it's common to create several objects which vary by only one or two attributes (particularly when asserting the order, or filtering, of entities):

it "sorts dogs oldest to youngest" do
  dog_1 = build(:dog, name: "Fido", age: 5)
  dog_2 = build(:dog, name: "Benji", age: 10)
  dog_3 = build(:dog, name: "Spot", age: 1)

  expect(DogSorter.new.sort).to eq [dog_2, dog_1, dog_3]
end

With FactoryBotVariants you could write this as:

dog_1, dog_2, dog_3 = build_variants(:dog, names: ["Fido", "Benji", "Spot"], ages: [5, 10, 1])

You can specify attribute names in plural if you like; names will become name as the attributes are passed to your factory.

If you have common attributes to assign to each variant, use the :all key:

build_variants(:dog, names: ["Brutus", "Avril"], all: { cute: false })

Installation

In your gemfile:

group :test do
  gem "factory_bot_variants"
end

Then bundle install and include the DSL where you'd like to use it:

In specific tests

require "factory_bot_variants/dsl"

RSpec.describe Dog do
  include FactoryBotVariants::DSL

  # Your spec
end

In all specs

require "factory_bot_variants/dsl"

RSpec.configure do |config|
  config.include FactoryBotVariants::DSL
end

API

There's an equivalent _variants method per FactoryBot strategy:

  • build_variants
  • create_variants
  • attributes_for_variants
  • build_stubbed_variants

Arguments:

  1. Factory name
  2. Traits (optional)
  3. One or more variant attributes

Example:

build_variants(
  factory_name,
  trait,
  attr1: [:a, :b],
  attr2: [1, 2],
  all: { common_attr: 1 },
)

About

Shorthand syntax for creating multiple variants with FactoryBot

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published