Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
tree: c0182b0bd4
Fetching contributors…

Cannot retrieve contributors at this time

84 lines (49 sloc) 2.294 kB

Watchmaker

Extract test setup and factory instantiation into reusable objects.

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

Define profiles

Specify car, garage and boat as a dependency for lots of things. Watchmaker will either use a watchmaker by that name to fulfill that dependency, or fall back to the factory if it doesn't exist.

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

Specify a block with things to do do:

Watchmaker.define :two_garages do
  2.times do 
    Factory.create :garage
  end
end

Specify a factory as a dependency:

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

Specify multiple factories as dependencies:

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

Pass those objects in to the block:

Watchmaker.define :car_and_driver, :factories => [:garage, :car] do |garage, car|
  garage.cars << car
end

Sepcify another watchmaker as a dependency:

Watchmaker.define :car, :watchmakers => [:garage]

Specify a factory and a watchmaker as a dependency:

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

Manufacture objects

Build in-memory objects using the two garages profile.

Watchmaker.build :two_garages

Build persisted objects using the two garages profile.

Watchmaker.create :two_garages

Get your objects back

Watchmaker returns the objects created.

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

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

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