CtrlK
BlogDocsLog inGet started
Tessl Logo

testland/factory-bot-data

Authors Ruby FactoryBot factories with traits, associations, sequences, and the three build strategies (build / create / build_stubbed); integrates with RSpec / Minitest test suites; pairs with Faker for randomized field values. Use when the project is Ruby / Rails and needs structured fixture creation with referential integrity.

75

Quality

94%

Does it follow best practices?

Run evals on this skill

Adds up to 20 points to the overall score

View guide

SecuritybySnyk

Passed

No findings from the security scan

Overview
Quality
Evals
Security
Files

strategies-and-anti-patterns.mdreferences/

FactoryBot build strategies and anti-patterns

Reference detail for factory-bot-data: the three build strategies and the factory anti-pattern catalog.

Build strategies

Per factory_bot-readme, FactoryBot supports three:

StrategyWhat it doesWhen to use
build(:x)Returns an unsaved ActiveRecord object.Pure-logic tests; no DB persistence needed.
create(:x)Saves the object (and any associations) to the database.Integration tests that exercise persistence.
build_stubbed(:x)Returns a stubbed object: appears persisted (new_record? returns false) but never hits the database.Speed up unit tests that don't actually need DB IO.

Speed hierarchy: build_stubbed >> build >> create. Use the weakest one that still tests what you need.

build(:user)          # Unsaved User instance
create(:user)         # Persisted User (and any associations)
build_stubbed(:user)  # Fake-persisted User; `id` is set, `new_record?` is false

Anti-patterns

Anti-patternWhy it failsFix
FactoryBot.create(:user) in every test, even for read-only assertionsDatabase overhead per test; suite slows linearly with test count.Use build_stubbed for unit tests that don't exercise persistence; reserve create for integration tests.
One mega-factory with 30 attributes in the baseEvery create(:user) writes 30 columns; tests that need 3 attributes pay the cost.Minimal base factory + traits for the rich variants.
Per-test-class factory definitionsTwo test files re-define :user differently; subtle bug.One factory per class, in spec/factories/ (auto-loaded by factory_bot_rails).
sequence for fields that should be random (e.g. names)Sequence values are predictable; tests pass on User 1 User 2 patterns that wouldn't pass on real names.Use Faker for value variety, sequence for uniqueness only.
create_list(:user, 100) per testDB write storm; even fast tests stall on bulk insert.Use build_stubbed_list if persistence isn't required; if it is, use raw SQL bulk insert.
Factory associations that always create the parentA test of Post creates a User; that creates 5 Permissions; etc. - explosion of objects.Pass an existing parent: create(:post, user: existing_user).

SKILL.md

tile.json