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 facto…
FixtureFactory is an attempt to merge concepts from Rails Fixtures and Factory Bot Factories to bridge the gap between factories and 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 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 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:
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:
class AccountTest { Account }) do
{ plan: :enterprise }
end
end
endThe 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:
parent:class RecipeTest { Recipe }, via: :recipes) do
{ name: "Cake" }
end
end
endThe 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:
class UserTest { User }, like: :bob, via: :users) do
{ role: :admin }
end
end
endThere 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:
class ActiveSupport::TestCase
define_factories do
factory(:address)
end
end
class AddressTest { email: "[email protected]", admin: true }
end
endattributes_for_list(name, count, overrides = {})Generates an array of hash attributes given a factory name, a count, and an optional hash of override attributes.
class BooksControllerTest [{ title: "Why's Poignant Guide to Ruby" }, { title: "Why's Poignant Guide to Ruby" }]
end
endbuild(name, overrides = {})Generates an unpersisted instance of a model given a factory name and an optional hash of override attributes.
class CourseTest #
end
endbuild_list(name, count, overrides = {})Generates an array of unpersisted model instances given a factory name and an optional hash of override attributes.
class PostTest [#, #]
end
endcreate(name, overrides = {})Generates a persisted model instance given a factory name and an optional hash of override attributes.
class CommentTest #
end
endcreate_list(name, count, overrides = {})Generates an array of persisted model instances given a factory name and an optional hash of override attributes.
class BlogTest [#, #]
end
endNo open issues yet, or sync has not completed.