Faking it with Machinist
 Create realistic, repeatable, datasets for testing and
                      development




Adam Dill          www.isotope11.com
Why?
• 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
Setup



          The Resets make
        them start over after
           each rollback
Machinist
            http://github.com/notahat/machinist
“create test data within your tests. It generates data for the
 fields you don't care about, and constructs any necessary
                      associated objects”

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.
blueprint.rb Shams
Sham: random but repeatable unique attributes values
a blueprint
        Call shams to generate data

                      or you can
                     specify fields
                      you always
                       to be the
                         same

Or create named blueprints,
 they inherent from their
     master blueprint
Creating
# make a generic User instance
User.make
User.make_unsaved
Post.make(:city => "Atlanta")

# make a named instance
User.make(:inactive_bob)
User.make_unsaved(:inactive_bob)

# or override a field
User.make(:city => "Atlanta")
AR Inheritance


Simple, or you can use over rides like before.
          I like writing test helpers
Faker
                         http://faker.rubyforge.org/



                             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 field)
Faker Example
seeds.rb



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
Adam Dill
  Senior Developer
 www.isotope11.com
adam@isotope11.com

Machinist

  • 1.
    Faking it withMachinist Create realistic, repeatable, datasets for testing and development Adam Dill www.isotope11.com
  • 2.
    Why? • Make testsshorter, 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
  • 3.
    Setup The Resets make them start over after each rollback
  • 4.
    Machinist http://github.com/notahat/machinist “create test data within your tests. It generates data for the fields you don't care about, and constructs any necessary associated objects” Like (and inspired by) Factory Girl, but with a cleaner DSL Extends active_record with make, make_unsaved
  • 5.
    make & make_unsaved Bothgenerate the object, but make_unsaved doesn’t call valid? or save or create, ect.
  • 6.
    blueprint.rb Shams Sham: randombut repeatable unique attributes values
  • 7.
    a blueprint Call shams to generate data or you can specify fields you always to be the same Or create named blueprints, they inherent from their master blueprint
  • 8.
    Creating # make ageneric User instance User.make User.make_unsaved Post.make(:city => "Atlanta") # make a named instance User.make(:inactive_bob) User.make_unsaved(:inactive_bob) # or override a field User.make(:city => "Atlanta")
  • 9.
    AR Inheritance Simple, oryou can use over rides like before. I like writing test helpers
  • 10.
    Faker http://faker.rubyforge.org/ 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
  • 11.
    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 field)
  • 12.
  • 13.
    seeds.rb Remember Account generatedusers 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
  • 14.
    Adam Dill Senior Developer www.isotope11.com adam@isotope11.com