Rome wasn't built in a day because they didn't have BlueprintsBoy
# Gemfile
gem 'blueprints_boy', group: :test
# spec/spec_helper.rb
BlueprintsBoy.enable
# spec/rome_spec.rb
it "builds rome in a day" do
start_time = Time.now
build :rome
(Time.now - start_time).should be < 1.day
rome.name.should == 'Rome'
end
# spec/blueprints.rb
blueprint :rome do
City.new('Rome')
end
# spec/rome_spec.rb
before do
build :rome
end
it "has colosseum" do
rome.buildings.size.should == 1
rome.buildings.first.should be_a(Building)
rome.buildings.first.name.should == 'Colosseum'
end
# spec/blueprints.rb
blueprint :colosseum do
Building.new('Colosseum')
end
depends_on(:colosseum).blueprint :rome do
City.new('Rome', buildings: [colosseum])
end
# spec/rome_spec.rb
it "has pizza" do
rome.food.size.should == 2
rome.food.each { |dish| dish.should be_a(Pizza) }
rome.food.map(&:name).should == ['Margherita', 'Marinara']
end
# spec/blueprints.rb
blueprint :luigi do
Cook.new 'Luigi'
end
depends_on :luigi do
blueprint :margherita do
luigi.bake('Margherita')
end
blueprint :marinara do
luigi.bake('Marinara')
end
end
# spec/blueprints.rb
blueprint :margherita, cook: luigi, name: 'Margherita' do |data|
data.attributes[:cook].bake(data.attributes[:name])
end
blueprint :marinara, cook: luigi, name: 'Marinara' do |data|
data.attributes[:cook].bake(data.attributes[:name])
end
build :antonio
build :margherita => {cook: antonio}
margherita.cook.should == antonio
# spec/blueprints.rb
attributes cook: luigi do
blueprint :margherita, name: 'Margherita' do |data|
data.attributes[:cook].bake(data.attributes[:name])
end
blueprint :marinara, name: 'Marinara' do |data|
data.attributes[:cook].bake(data.attributes[:name])
end
end
# spec/blueprints.rb
BlueprintsBoy.factories.add(Pizza, :create) do |data|
data.attributes[:cook].bake(data.attributes[:name])
end
factory(Pizza).attributes cook: luigi do
blueprint :margherita, name: 'Margherita'
blueprint :marinara, name: 'Marinara'
end
# spec/blueprints.rb
depends_on(:colosseum, :margherita, :marinara).blueprint :rome do
City.new('Rome', buildings: [colosseum], food: [margherita, marinara])
end
# spec/blueprints.rb
group :pizzas => [:margherita, :marinara]
depends_on(:colosseum, :pizzas).blueprint :rome do
City.new('Rome', buildings: [colosseum], food: pizzas)
end
BlueprintsBoy defines default factories for ActiveRecord and Mongoid
# app/models/user.rb
class User < ActiveRecord::Base
end
# spec/blueprints.rb
factory(User).blueprint :admin, email: 'admin@example.com'
# spec/rome_spec.rb
it "creates user" do
build :admin
admin.should be_a(User)
admin.should be_persisted
admin.email.should == 'admin@example.com'
end