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

fixture_factory

> 编程语言
Open source

FixtureFactory FixtureFactory is an attempt to merge concepts from Rails Fixtures and Factory Bot Factories to bridge the gap between facto…

100 stars0 likes0 views
WebsiteGitHub

About

FixtureFactory FixtureFactory is an attempt to merge concepts from Rails Fixtures and Factory Bot Factories to bridge the gap between facto…

FixtureFactory

FixtureFactory is an attempt to merge concepts from Rails Fixtures and Factory Bot Factories to bridge the gap between factories and fixtures.

Fixtures

Fixtures are fast, simple, and an easy way to seed your test database with sample data. Codebases that leverage fixtures often have faster test runs than factory-based test suites. This is due to fixtures being loaded once whereas factories build models for each test. Fixtures are also officially recommended over factories by the Rails core team.

Fixtures start to become a pain when you need to test models with complex state. Unlike factories, there's no simple way to ask for a model, transform it, and start using it in one line (without using a helper method or similar).

For more information on fixtures, check out the Rails guides.

Factories

Factories make testing any model nearly painless. State differences are expressed in factory definitions through traits, callbacks, and transient attributes. Factories are often paired with data generating libraries to add an extra degree of verification to your tests.

As mentioned above, factories' biggest downside is the speed trade-off of building models for each test. There are also added compatibility concerns with factories as they are not the officially recommended way of testing Rails apps.

For more information on factories, check out FactoryBot's wiki.

Fixture Factories

Fixture Factories is not a fixture replacement. Rather, they are meant to compliment an existing suite of fixtures. When testing models with complex state, a fixture factory can:

  • Provide a simple creation syntax that supports overrides
  • Use existing fixtures as templates for fixtures factories
  • Act as an alternative to redundant fixture definitions

Definition

FixtureFactory definitions can be made by anything that includes FixtureFactory::Registry. Typically, this is a test case class. Definitions are made with the .define_factories and .factory method:

ruby
class AccountTest  { Account }) do
      { plan: :enterprise }
    end
  end
end

Naming

The name of your fixture is important. It infers the class (with ActiveSupport::Inflector) you'll be using, and the fixture method you source fixtures from. Fixture with non-standard names can get around this problem 2 different ways:

  1. Define a base fixture with a simple name and extend via parent:
ruby
class RecipeTest  { Recipe }, via: :recipes) do
      { name: "Cake" }
    end
  end
end

Fixtures

The whole point of fixture factories is to complement a fixture suite. Typically, you'll want to link your factories to fixtures. This is done with the via and like options:

ruby
class UserTest  { User }, like: :bob, via: :users) do
      { role: :admin }
    end
  end
end

Inheritance

There are two aspects of inheritance to factories definitions. Inheritance at the registry level, and inheritance at the definition level. Registry subclasses inherit definitions from their superclass. Fixture factory definitions can specify a parent factory to inherit options from. Here's an example:

ruby
class ActiveSupport::TestCase
  define_factories do
    factory(:address)
  end
end

class AddressTest  { email: "[email protected]", admin: true }
  end
end

attributes_for_list(name, count, overrides = {})

Generates an array of hash attributes given a factory name, a count, and an optional hash of override attributes.

ruby
class BooksControllerTest  [{ title: "Why's Poignant Guide to Ruby" }, { title: "Why's Poignant Guide to Ruby" }]
  end
end

build(name, overrides = {})

Generates an unpersisted instance of a model given a factory name and an optional hash of override attributes.

ruby
class CourseTest  #
  end
end

build_list(name, count, overrides = {})

Generates an array of unpersisted model instances given a factory name and an optional hash of override attributes.

ruby
class PostTest  [#, #]
  end
end

create(name, overrides = {})

Generates a persisted model instance given a factory name and an optional hash of override attributes.

ruby
class CommentTest  #
  end
end

create_list(name, count, overrides = {})

Generates an array of persisted model instances given a factory name and an optional hash of override attributes.

ruby
class BlogTest  [#, #]
  end
end

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 推出的简洁高效系统语言