Permalink
Browse files

Added a blueprint syntax layer

  • Loading branch information...
jferris committed Feb 17, 2009
1 parent 03b3f92 commit de83c9cf8cede547cc09126f67ffe00ebd307f54
Showing with 79 additions and 0 deletions.
  1. +40 −0 lib/factory_girl/syntax/blueprint.rb
  2. +39 −0 test/syntax/blueprint.rb
@@ -0,0 +1,40 @@
class Factory
module Syntax #:nodoc:
# Extends ActiveRecord::Base to provide a make class method, which is an
# alternate syntax for defining factories.
#
# Usage:
#
# require 'factory_girl/syntax/blueprint'
#
# User.blueprint do
# name { 'Billy Bob' }
# email { 'billy@bob.example.com' }
# end
#
# Factory(:user, :name => 'Johnny')
module Blueprint
module ActiveRecord #:nodoc:
def self.included(base) # :nodoc:
base.extend ClassMethods
end
module ClassMethods #:nodoc:
def blueprint(&block)
instance = Factory.new(name.underscore, :class => self)
instance.instance_eval(&block)
Factory.factories[instance.factory_name] = instance
end
end
end
end
end
end
ActiveRecord::Base.send(:include, Factory::Syntax::Blueprint::ActiveRecord)
View
@@ -0,0 +1,39 @@
require 'test_helper'
require 'factory_girl/syntax/blueprint'
class BlueprintSyntaxTest < Test::Unit::TestCase
context "a blueprint" do
setup do
Factory.sequence(:email) { |n| "somebody#{n}@example.com" }
User.blueprint do
first_name { 'Bill' }
last_name { 'Nye' }
email { Factory.next(:email) }
end
end
teardown do
Factory.factories.clear
Factory.sequences.clear
end
context "after making an instance" do
setup do
@instance = Factory(:user, :last_name => 'Rye')
end
should "use attributes from the blueprint" do
assert_equal 'Bill', @instance.first_name
end
should "evaluate attribute blocks for each instance" do
assert_match /somebody\d+@example.com/, @instance.email
assert_not_equal @instance.email, Factory(:user).email
end
end
end
end

0 comments on commit de83c9c

Please sign in to comment.