Faking it with Machinist
Create realistic, repeatable, datasets for testing and
Adam Dill www.isotope11.com
• Make tests shorter, simpler
• Dependable development data
• Easy to make changes
• rake db:reset calls db/seeds.rb
• works great in console for playing with
requests: u = user.make
The Resets make
them start over after
“create test data within your tests. It generates data for the
ﬁelds you don't care about, and constructs any necessary
Like (and inspired by) Factory Girl, but with a cleaner DSL
Extends active_record with make, make_unsaved
make & make_unsaved
Both generate the object, but make_unsaved doesn’t
call valid? or save or create, ect.
Sham: random but repeatable unique attributes values
Call shams to generate data
or you can
to be the
Or create named blueprints,
they inherent from their
# make a generic User instance
Post.make(:city => "Atlanta")
# make a named instance
# or override a ﬁeld
User.make(:city => "Atlanta")
Simple, or you can use over rides like before.
I like writing test helpers
A port of Perl‘s
Data::Faker library that
generates fake data.
How much Fake could a faker fake if a faker could ... forget it
How much Fake?
Notice the uniques on the shams, this is a gotcha
after you generate all possible (or run out of
Faker data) then it will error out.
if this is a problem, add an index (see the email ﬁeld)
Remember Account generated users also.
so we have 100 users with accounts
in our development.rb
that aside from random generators
are the _exact_ same every time.
Its also fast