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