Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Fetching contributors…
Cannot retrieve contributors at this time
94 lines (54 sloc) 2.34 KB


Build complex objects easily for use in integration tests.


Extract complex setup code for integration tests out into a central place; for example:

  • Share setup code between cucumber and test/unit by centralizing it.

  • Run this code in the console in development mode to seed your db with test data.



Watchmaker creates persisted objects only. This is because these objects are meant for integration tests where persistence is required.

Learn profiles

Implicit profiles

Specify a dependency hash of profile name to objects. The watchmaker will either yield another watchmaker or a factory when resolving those dependencies.

Watchmaker.learn :lots_of_things => [:car, :garage, :boat]

Lambda-based profiles

When called, will call the lambda.

Watchmaker.learn :two_garages do
  2.times do 
    Factory.create :garage

Factory-based profiles

Create a garage using the garage factory.

Watchmaker.learn :garage, :factories => [:garage] do
  # Some other post-factory creation setup here.

Watchmaker-based profiles

Create a garage using the garage factory.

Watchmaker.learn :garage, :factories => [:garage]

Create a car using the car factory, and a garage using the garage watchmaker.

Watchmaker.learn :car, :factories => [:car], :watchmakers => [:garage]

Inject created objects into the lambda

Inject the factory-created garage and car into the block.

Watchmaker.learn :car_in_garage, :factories => [:garage, :car] do |garage, car| << car

Grab your objects

Watchmaker returns the objects created.

Watchmaker.construct(:garage).first.class # Garage

Construct objects

Build objects using the two garages profile.

Watchmaker.construct :two_garages

Build objects using the garage profile.

Watchmaker.construct :garages

Inspiration for the name

William Paley's Watchmaker Analogy and Argument.


Watchmaker is Copyright © 2011 Christopher Meiklejohn. It is free software, and may be redistributed under the terms specified in the LICENSE file.


The watchmaker gem was written by Christopher Meiklejohn from Swipely, Inc..

Jump to Line
Something went wrong with that request. Please try again.