Permalink
Browse files

Added a generate syntax layer

  • Loading branch information...
jferris committed Feb 17, 2009
1 parent deeed51 commit 451737ab4939116605a44bf30a8f7beb667abe23
Showing with 131 additions and 0 deletions.
  1. +68 −0 lib/factory_girl/syntax/generate.rb
  2. +63 −0 test/syntax/generate_test.rb
@@ -0,0 +1,68 @@
class Factory
module Syntax #:nodoc:
# Extends ActiveRecord::Base to provide generation methods for factories.
#
# Usage:
#
# require 'factory_girl/syntax/generate'
#
# Factory.define :user do |factory|
# factory.name 'Billy Bob'
# factory.email 'billy@bob.example.com'
# end
#
# # Creates a saved instance without raising (same as saving the result
# # of Factory.build)
# User.generate(:name => 'Johnny')
#
# # Creates a saved instance and raises when invalid (same as
# # Factory.create)
# User.generate!
#
# # Creates an unsaved instance (same as Factory.build)
# User.spawn
#
# # Creates an instance and yields it to the passed block
# User.generate do |user|
# # ...do something with user...
# end
#
# This syntax was derived from Rick Bradley and Yossef Mendelssohn's
# object_daddy.
module Generate
module ActiveRecord #:nodoc:
def self.included(base) # :nodoc:
base.extend ClassMethods
end
module ClassMethods #:nodoc:
def generate(overrides = {}, &block)
instance = Factory.build(name.underscore, overrides)
instance.save
yield(instance) if block_given?
instance
end
def generate!(overrides = {}, &block)
instance = Factory.create(name.underscore, overrides)
yield(instance) if block_given?
instance
end
def spawn(overrides = {}, &block)
instance = Factory.build(name.underscore, overrides)
yield(instance) if block_given?
instance
end
end
end
end
end
end
ActiveRecord::Base.send(:include, Factory::Syntax::Generate::ActiveRecord)
@@ -0,0 +1,63 @@
require 'test_helper'
require 'factory_girl/syntax/generate'
class GenerateSyntaxTest < Test::Unit::TestCase
context "a factory" do
setup do
Factory.define :user do |factory|
factory.first_name 'Bill'
factory.last_name 'Nye'
factory.email 'science@guys.net'
end
end
teardown do
Factory.factories.clear
end
should "not raise an error when generating an invalid instance" do
assert_nothing_raised { User.generate(:first_name => nil) }
end
should "raise an error when forcefully generating an invalid instance" do
assert_raise ActiveRecord::RecordInvalid do
User.generate!(:first_name => nil)
end
end
%w(generate generate! spawn).each do |method|
should "yield a generated instance when using #{method} with a block" do
instance = nil
User.send(method) {|instance| }
assert_kind_of User, instance
end
context "after generating an instance using #{method}" do
setup do
@instance = User.send(method, :last_name => 'Rye')
end
should "use attributes from the factory" do
assert_equal 'Bill', @instance.first_name
end
should "use attributes passed to generate" do
assert_equal 'Rye', @instance.last_name
end
if method == 'spawn'
should "not save the record" do
assert @instance.new_record?
end
else
should "save the record" do
assert !@instance.new_record?
end
end
end
end
end
end

0 comments on commit 451737a

Please sign in to comment.