Watchmaker

Build complex objects easily for use in integration tests.

Motivation

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.

Using

Basics

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
  end
end

Factory-based profiles

Create a garage using the garage factory.

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

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| 
  garage.cars << car
end

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.

License

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

About

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