Namespaces
Blueprints provides you with ability to group blueprints using namespaces. So for example if you have two blueprints:
blueprint :red_apple do
Fruit.create!(:species => 'apple')
end
blueprint :red_cherry do
Fruit.create!(:species => 'cherry')
end
You could put them into namespace like this:
namespace :red do
blueprint :apple do
Fruit.create!(:species => 'apple')
end
blueprint :cherry do
Fruit.create!(:species => 'cherry')
end
end
And then build in test case like this:
build 'red.apple'
@red_apple.should_not be_nil
You could take even build whole namespace at once like this:
build 'red'
@red.should == [@red_apple, @red_cherry]
This gives you additional bonus @red instance variable which contains results of all children blueprints blocks, so in this case it would contain @red_apple and @red_cherry.
Attributes and dependencies
Blueprints also inherit all attributes and dependencies of their namespaces.
# In blueprints file
attributes(:color => 'red').namespace :red do
Fruit.blueprint :apple, :name => 'apple'
end
# In test case
build 'red.apple'
@red_apple.color.should == 'red'
Default blueprint
If you don't like default strategy when building namespace, you can always override it with default blueprint. Default
blueprint looks the same as any other blueprint, except it is named :default
.
defined like this:
# In blueprints file
namespace :fruits do
blueprint :default do
Fruit.all
end
end
# In test case
build :fruits
@fruits.should == Fruit.all