Skip to content

Commit

Permalink
Added default strategy parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
eugenebolshakov authored and jferris committed Feb 17, 2009
1 parent 517cc94 commit 1e1c22d
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 10 deletions.
2 changes: 1 addition & 1 deletion lib/factory_girl.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
# Example: # Example:
# Factory(:user, :name => 'Joe') # Factory(:user, :name => 'Joe')
def Factory (name, attrs = {}) def Factory (name, attrs = {})
Factory.create(name, attrs) Factory.default_strategy(name, attrs)
end end


if defined? Rails if defined? Rails
Expand Down
21 changes: 18 additions & 3 deletions lib/factory_girl/factory.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ def class_name #:nodoc
def build_class #:nodoc: def build_class #:nodoc:
@build_class ||= class_for(class_name) @build_class ||= class_for(class_name)
end end

def default_strategy #:nodoc:
@options[:default_strategy] || :create
end


def initialize (name, options = {}) #:nodoc: def initialize (name, options = {}) #:nodoc:
assert_valid_options(options) assert_valid_options(options)
Expand Down Expand Up @@ -170,7 +174,7 @@ def sequence (name, &block)
s = Sequence.new(&block) s = Sequence.new(&block)
add_attribute(name) { s.next } add_attribute(name) { s.next }
end end

# Generates and returns a Hash of attributes from this factory. Attributes # Generates and returns a Hash of attributes from this factory. Attributes
# can be individually overridden by passing in a Hash of attribute => value # can be individually overridden by passing in a Hash of attribute => value
# pairs. # pairs.
Expand Down Expand Up @@ -230,7 +234,11 @@ def self.create (name, overrides = {})
# A mock object with generated attributes stubbed out (Object) # A mock object with generated attributes stubbed out (Object)
def self.stub (name, overrides = {}) def self.stub (name, overrides = {})
factory_by_name(name).run(Proxy::Stub, overrides) factory_by_name(name).run(Proxy::Stub, overrides)
end end

def self.default_strategy (name, overrides = {})
self.send(factory_by_name(name).default_strategy, name, overrides)
end


def self.find_definitions #:nodoc: def self.find_definitions #:nodoc:
definition_file_paths.each do |path| definition_file_paths.each do |path|
Expand Down Expand Up @@ -284,10 +292,17 @@ def attribute_defined? (name)
end end


def assert_valid_options(options) def assert_valid_options(options)
invalid_keys = options.keys - [:class, :parent] invalid_keys = options.keys - [:class, :parent, :default_strategy]
unless invalid_keys == [] unless invalid_keys == []
raise ArgumentError, "Unknown arguments: #{invalid_keys.inspect}" raise ArgumentError, "Unknown arguments: #{invalid_keys.inspect}"
end end
assert_valid_strategy(options[:default_strategy]) if options[:default_strategy]
end

def assert_valid_strategy(strategy)
unless Factory::Proxy.const_defined? variable_name_to_class_name(strategy)
raise ArgumentError, "Unknown strategy: #{strategy}"
end
end end


# Based on ActiveSupport's underscore inflector # Based on ActiveSupport's underscore inflector
Expand Down
33 changes: 29 additions & 4 deletions test/factory_test.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -46,13 +46,17 @@ class FactoryTest < Test::Unit::TestCase
should "have a build class" do should "have a build class" do
assert_equal @class, @factory.build_class assert_equal @class, @factory.build_class
end end

should "have a default strategy" do
assert_equal :create, @factory.default_strategy
end


should "not allow the same attribute to be added twice" do should "not allow the same attribute to be added twice" do
assert_raise(Factory::AttributeDefinitionError) do assert_raise(Factory::AttributeDefinitionError) do
2.times { @factory.add_attribute :first_name } 2.times { @factory.add_attribute :first_name }
end end
end end

should "add a static attribute when an attribute is defined with a value" do should "add a static attribute when an attribute is defined with a value" do
attribute = mock('attribute', :name => :name) attribute = mock('attribute', :name => :name)
Factory::Attribute::Static. Factory::Attribute::Static.
Expand Down Expand Up @@ -238,7 +242,7 @@ class FactoryTest < Test::Unit::TestCase
assert_nil @result[:test] assert_nil @result[:test]
end end
end end

should "guess the build class from the factory name" do should "guess the build class from the factory name" do
assert_equal User, @factory.build_class assert_equal User, @factory.build_class
end end
Expand Down Expand Up @@ -362,8 +366,18 @@ class FactoryTest < Test::Unit::TestCase
returns('result') returns('result')
assert_equal 'result', Factory.stub(@name, :attr => 'value') assert_equal 'result', Factory.stub(@name, :attr => 'value')
end end

should "use default strategy option as Factory.default_strategy" do
@factory.stubs(:default_strategy).returns(:create)
@factory.
expects(:run).
with(Factory::Proxy::Create, :attr => 'value').
returns('result')
assert_equal 'result', Factory.default_strategy(@name, :attr => 'value')
end


should "use Proxy::Create for the global Factory method" do should "use the default strategy for the global Factory method" do
@factory.stubs(:default_strategy).returns(:create)
@factory. @factory.
expects(:run). expects(:run).
with(Factory::Proxy::Create, :attr => 'value'). with(Factory::Proxy::Create, :attr => 'value').
Expand All @@ -384,7 +398,7 @@ class FactoryTest < Test::Unit::TestCase
end end
end end


context 'defining a factory using a parent attribute' do context 'defining a factory with a parent parameter' do
setup do setup do
@parent = Factory.define :object do |f| @parent = Factory.define :object do |f|
f.name 'Name' f.name 'Name'
Expand Down Expand Up @@ -422,6 +436,17 @@ class FactoryTest < Test::Unit::TestCase
end end
end end


context 'defining a factory with a default strategy parameter' do
should 'raise an ArgumentError when trying to use a non-existent factory' do
assert_raise(ArgumentError) { Factory.define(:object, :default_strategy => :nonexistent) {} }
end

should 'create a new factory with a specified default strategy' do
factory = Factory.define(:object, :default_strategy => :stub) {}
assert_equal :stub, factory.default_strategy
end
end

def self.context_in_directory_with_files(*files) def self.context_in_directory_with_files(*files)
context "in a directory with #{files.to_sentence}" do context "in a directory with #{files.to_sentence}" do
setup do setup do
Expand Down
10 changes: 8 additions & 2 deletions test/integration_test.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def setup
f.email {|a| "#{a.first_name}.#{a.last_name}@example.com".downcase } f.email {|a| "#{a.first_name}.#{a.last_name}@example.com".downcase }
end end


Factory.define Post do |f| Factory.define Post, :default_strategy => :attributes_for do |f|
f.name 'Test Post' f.name 'Test Post'
f.association :author, :factory => :user f.association :author, :factory => :user
end end
Expand Down Expand Up @@ -131,7 +131,7 @@ def teardown
end end


end end

context "an instance generated by a factory with a custom class name" do context "an instance generated by a factory with a custom class name" do


setup do setup do
Expand Down Expand Up @@ -225,5 +225,11 @@ def teardown
end end


end end

context "a factory with a default strategy specified" do
should "generate instances according to the strategy" do
assert_kind_of Hash, Factory(:post)
end
end


end end

0 comments on commit 1e1c22d

Please sign in to comment.