FakerMaker generates factories that build disposable objects for testing. Each factory has a name and a set of attributes.

FakerMaker.factory :user do
  name {'Patsy Stone'}
  email {'[email protected]'}
  admin {false}
end

This will generate a FakerMaker::Factory::User class with the attributes name, email and admin which will always return the same value. Since v3.0.0 Classes generated by FakerMaker are in the FakerMaker::Factory namespace.

It is possible to explicitly set the name of class which is particularly useful if there is a risk of redefining an existing one. Classes will always be created within the FakerMaker::Factory namespace (since v3.0.0)

FakerMaker.factory :user, class: 'EmailUser' do
  name {'Patsy Stone'}
  email {'[email protected]'}
  admin {false}
end

The class name will always be turned into a Ruby-style class name so email_user would become FakerMaker::Factory::EmailUser.

Because of the block syntax in Ruby, defining attributes as Hashes requires two sets of curly brackets:

FakerMaker.factory :response do
  body { { title: 'First Post', content: 'This is part of a hash' } }
end

Blocks are executed in the context of their instance. This means you can refer to variables already defined:

FakerMaker.factory :user, class: 'EmailUser' do
  title {'Ms'}
  name {'Patsy Stone'}
  formal_name {"#{title} #{name}"}
  email {'[email protected]'}
  admin {false}
end

Fields with no block (or reference to another factory) will be nil.

FakerMaker.factory :request do
  body
end

FakerMaker[:request].build.body
# => nil