Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Extract test setup and factory instantiation into reusable objects.
tree: 2c40e8e3c1

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
db
gemfiles
lib
spec
.gitignore
.rspec
.rvmrc
.travis.yml
Appraisals
Gemfile
Guardfile
LICENSE
README.rdoc
Rakefile
watchmaker.gemspec

README.rdoc

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

Something went wrong with that request. Please try again.