Blueprints boy

The ultimate solution to managing test data.

View project on GitHub

Using factories

Factories allows you to extract common functionality from blueprints building. For example let’s say you have:

blueprint :apple do
  Fruit.new 'apple'
end

blueprint :orange do
  Fruit.new 'orange'
end

Instead of all that you could extract a factory and all the code would look like this:

BlueprintsBoy.factories.add(Fruit, :create) { |data| data.factory.new(data.attributes[:name]) }

factory(Fruit).blueprint :apple, name: 'apple'
factory(Fruit).blueprint :orange, name: 'orange'

Where :create is a strategy that this factory is defined for. This can then be shortened even more by:

BlueprintsBoy.factories.add(Fruit, :create) { |data| data.factory.new(data.attributes[:name]) }

factory Fruit do
  blueprint :apple, name: 'apple'
  blueprint :orange, name: 'orange'
end

This same factory would even work for subclasses of Fruit! So you could write factories for your own ORMs. For examples see ActiveRecord factories and Mongoid factories

ORM factories

Since they are so widely used, BlueprintsBoy provides factories for ActiveRecord (>= 3.1) and Mongoid (>= 2.0) out of the box.

# Somewhere in application
class Fruit < ActiveRecord::Base
end

# Blueprints file
factory(Fruit).blueprint :orange, name: 'orange'