Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
F

fixturebot

> 编程语言
Open source

The speed of fixtures with the syntactic sugar of factories.

199 stars0 likes1 views
WebsiteGitHub

About

The speed of fixtures with the syntactic sugar of factories.

FixtureBot

The syntactic sugar of factories with the speed of fixtures.

FixtureBot lets you define your test data in a Ruby DSL and compiles it into standard Rails fixture YAML files. The generated YAML is deterministic and should be checked into git, just like a lockfile. Your tests never see FixtureBot at runtime; Rails just loads the YAML fixtures as usual.

Features:

  • Ruby DSL for defining records, associations, and join tables
  • Generators for filling in required columns (like email) across all records
  • Stable IDs so foreign keys are consistent and diffs are clean across runs
  • UUID support for tables with UUID primary keys
  • Polymorphic associations with automatic type column resolution
  • Schema auto-detection from your Rails database (no manual column lists)
  • Auto-generates before your test suite runs (RSpec and Minitest)

Read the article

More background at BeautifulRuby.com.

Quick example

…

This generates YAML files like users.yml, posts.yml, tags.yml, and posts_tags.yml in your fixtures directory. Use them in tests like any other fixture:

ruby
# RSpec
RSpec.describe Post, type: :model do
  fixtures :all

  it "belongs to an author" do
    post = posts(:hello_world)
    expect(post.author).to eq(users(:brad))
  end
end

# Minitest
class PostTest  { id: , email: "[email protected]" }
end

Generators

Generators set default column values. They run for each record that doesn't explicitly set that column. Generators are never created implicitly; columns without a value or generator are omitted from the YAML output (Rails uses the database column default).

ruby
FixtureBot.define do
  user.email { |fixture| "#{fixture.key}@example.com" }
end

The generator block receives a fixture object with a key method (the record's symbol name). Bare methods inside the block refer to column values set on the record:

ruby
FixtureBot.define do
  user.display_name { |fixture| "#{fixture.key} (#{email})" }
end

When a generator covers all the columns you need, records don't need a block at all:

ruby
FixtureBot.define do
  tag.name { |fixture| fixture.key.to_s.capitalize }

  tag :ruby     # name: "Ruby"
  tag :rails    # name: "Rails"
  tag :testing  # name: "Testing"
end

A literal value shadows the generator. An explicit nil also shadows it:

ruby
FixtureBot.define do
  user.email { |fixture| "#{fixture.key}@example.com" }

  user :brad do
    name "Brad"
    email "[email protected]"  # literal, skips the generator
  end

  user :alice do
    name "Alice"
    # no email set, generator produces "alice@example.com"
  end

  user :deactivated do
    name "Ghost"
    email nil              # explicit nil, skips the generator, sets email to null
  end
end

Associations

Reference other records by name for belongs_to:

ruby
FixtureBot.define do
  post :hello_world do
    title "Hello World"
    author :brad  # sets author_id to brad's stable ID
  end
end

Polymorphic associations

For polymorphic belongs_to, reference the target using its table helper to set both _id and _type columns:

ruby
FixtureBot.define do
  post :hello do
    title "Hello"
  end

  comment :nice do
    body "Nice post"
  end

  vote :upvote_post do
    votable post(:hello)       # sets votable_id and votable_type: "Post"
  end

  vote :upvote_comment do
    votable comment(:nice)     # sets votable_id and votable_type: "Comment"
  end
end

In the manual schema, declare polymorphic associations with polymorphic:

ruby
FixtureBot::Schema.define do
  table :posts, singular: :post, columns: [:title]
  table :comments, singular: :comment, columns: [:body]

  table :votes, singular: :vote, columns: [:votable_id, :votable_type, :voter_id] do
    polymorphic :votable
    belongs_to :voter, table: :users
  end
end

In Rails, polymorphic associations are auto-detected from _id/_type column pairs that don't have a foreign key constraint.

Join tables (HABTM)

Reference multiple records for join table associations:

ruby
FixtureBot.define do
  post :hello_world do
    title "Hello World"
    tags :ruby, :rails  # creates rows in posts_tags
  end
end

Hardcoded IDs

By default, FixtureBot generates stable IDs deterministically from the table and record name. You can override this with an explicit value:

ruby
FixtureBot.define do
  user :admin do
    id 42
    name "Admin"
  end

  post :hello do
    title "Hello"
    author :admin  # author_id resolves to 42
  end
end

UUID primary keys

Tables with UUID primary keys work automatically in Rails (detected from the column type). In the manual schema, pass key: FixtureBot::Key::Uuid:

ruby
FixtureBot::Schema.define do
  table :projects, singular: :project, columns: [:name], key: FixtureBot::Key::Uuid
end

FixtureBot generates deterministic UUID v5 values, so the output is stable across runs.

You can also provide your own key strategy — any object (or module) that responds to generate(table_name, record_name). For example, Stripe-style prefixed IDs:

ruby
module PrefixedKey
  module_function

  def generate(table_name, record_name)
    hash = Zlib.crc32("#{table_name}:#{record_name}").to_s(36)
    "#{table_name.to_s.chomp('s')}_#{hash}"
  end
end

FixtureBot::Schema.define do
  table :customers, singular: :customer, columns: [:name], key: PrefixedKey
  # => customer_id: "customer_1a2b3c"
end

Custom primary key column

If your table uses a column other than id as the primary key:

ruby
FixtureBot::Schema.define do
  table :users, singular: :user, columns: [:name], primary_key: :uid
end

In Rails, this is auto-detected from the database.

Multiple files

For larger apps, split fixtures across multiple files using FixtureBot.require:

ruby
# spec/fixtures.rb
FixtureBot.require "spec/fixtures/**/*.rb"

FixtureBot.define do
  user.email { |fixture| "#{fixture.key}@example.com" }
end
ruby
# spec/fixtures/users.rb
FixtureBot.define do
  user :brad do
    name "Brad"
  end
end
ruby
# spec/fixtures/posts.rb
FixtureBot.define do
  post :hello do
    title "Hello"
    author :brad
  end
end

Each file calls FixtureBot.define with its own block. Files are loaded in alphabetical order. References across files work because everything is resolved after all files are loaded.

Implicit vs explicit style

By default, the block is evaluated implicitly. Table methods like user and post are available directly:

ruby
FixtureBot.define do
  user :brad do
    name "Brad"
  end
end

If you prefer an explicit receiver (useful for editor autocompletion or clarity in large files), pass a block argument:

ruby
FixtureBot.define do |t|
  t.user :brad do
    name "Brad"
  end
end

Both styles are equivalent. Record blocks (the inner do...end) are always implicit.

Schema

FixtureBot reads your database schema automatically in Rails. Outside of Rails, you can define it by hand:

ruby
FixtureBot::Schema.define do
  table :users, singular: :user, columns: [:name, :email]

  table :posts, singular: :post, columns: [:title, :body, :author_id] do
    belongs_to :author, table: :users
  end

  table :tags, singular: :tag, columns: [:name]

  join_table :posts_tags, :posts, :tags
end

Migrating from FactoryBot

FixtureBot provides build, create, attributes_for, and related methods that mirror FactoryBot's API. The key difference is that you pass both a table name and a fixture name instead of just a factory name:

…

Method reference

Method Behavior
build(:user, :brad, **attrs) Duplicates the fixture, applies overrides. Returns unpersisted.
create(:user, :brad, **attrs) Without overrides: returns the fixture (already persisted). With overrides: build + save!.
build_stubbed(:user, :brad, **attrs) Like build but retains id. Looks persisted without touching DB.
attributes_for(:user, :brad, **attrs) Returns attributes hash, strips id/created_at/updated_at.
build_list(:user, :brad, :alice, **attrs) Maps each name through build.
create_list(:user, :brad, :alice, **attrs) Maps each name through create.
build_pair(:user, :brad, :alice, **attrs) Alias for build_list with 2 names.
create_pair(:user, :brad, :alice, **attrs) Alias for create_list with 2 names.
build_stubbed_list(:user, :brad, :alice, **attrs) Maps each name through build_stubbed.
build_stubbed_pair(:user, :brad, :alice, **attrs) Alias for build_stubbed_list with 2 names.

These methods are automatically available in your tests when you require fixturebot/rspec or fixturebot/minitest. They call the standard Rails fixture accessors under the hood, so build(:user, :brad) is equivalent to users(:brad).dup.

Prior art

Rails fixtures

Rails fixtures are YAML files that get loaded into the database once before your test suite runs. Each test wraps in a transaction and rolls back, so the data is always clean and tests are fast. This is the approach FixtureBot builds on.

The problem is writing YAML by hand. Foreign keys are magic strings (author: brad), there's no way to DRY up repeated columns, and large fixture files are hard to read. FixtureBot gives you a Ruby DSL that compiles down to the same YAML, so you keep the speed of fixtures without the pain of maintaining them.

FactoryBot

FactoryBot creates records on the fly inside each test. You call create(:user) and it inserts a row. This makes tests self-contained and easy to read, but it's slow. Every test that needs data pays the cost of inserting records, and complex object graphs lead to cascading create calls.

FixtureBot borrows the DSL feel of FactoryBot (named records, associations by symbol, default generators) but compiles to fixtures instead of inserting at runtime. You get the ergonomics of factories with the speed of fixtures.

Oaken

Oaken and FixtureBot share the same motivation: replace hand-written YAML fixtures with a Ruby DSL. They take very different approaches.

Oaken inserts records into the database at runtime using ActiveRecord::Base#create!. It also supports loading different seed files per test case (seed "cases/pagination"), which means your data set can vary across tests. This flexibility comes at a cost: you lose the "load once, wrap every test in a transaction" speed advantage that makes Rails fixtures fast. It's closer to factories in that regard, with more structure around organizing seed scripts.

FixtureBot is more opinionated. One fixture file, one data set, compiled to plain YAML and checked into git. At test time, FixtureBot is out of the picture entirely. Rails loads the YAML fixtures once and wraps each test in a transaction as usual. No runtime dependency, no per-test seeding, no seed file organization to manage.

FixtureBot Rails fixtures FactoryBot Oaken
Define data in Ruby DSL YAML Ruby DSL Ruby scripts
Output YAML files in git YAML files in git Database rows per test Database rows at boot
Runtime dependency None None Required per test Required at boot
Data set One set, loaded once One set, loaded once Built per test Per-test via seed files
Speed Fast (fixtures) Fast (fixtures) Slow (inserts per test) Varies
Stable IDs Deterministic Deterministic Database-assigned Database-assigned

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt.

Try the playground without Rails:

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

Ruby

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 18, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言