Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Extract test setup and factory instantiation into reusable objects.

branch: master

Fetching latest commit…

Octocat-spinner-32-eaf2f5

Cannot retrieve the latest commit at this time

Octocat-spinner-32 db
Octocat-spinner-32 gemfiles
Octocat-spinner-32 lib
Octocat-spinner-32 spec
Octocat-spinner-32 .gitignore
Octocat-spinner-32 .rspec
Octocat-spinner-32 .rvmrc
Octocat-spinner-32 .travis.yml
Octocat-spinner-32 Appraisals
Octocat-spinner-32 Gemfile
Octocat-spinner-32 Gemfile.lock
Octocat-spinner-32 Guardfile
Octocat-spinner-32 LICENSE
Octocat-spinner-32 README.rdoc
Octocat-spinner-32 Rakefile
Octocat-spinner-32 watchmaker.gemspec
README.rdoc

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

Something went wrong with that request. Please try again.