Permalink
Browse files

Added default strategy parameter

  • Loading branch information...
eugenebolshakov authored and jferris committed Jan 8, 2009
1 parent 517cc94 commit 1e1c22d06e5e7561bf649194503f29f604ef2e9a
Showing with 56 additions and 10 deletions.
  1. +1 −1 lib/factory_girl.rb
  2. +18 −3 lib/factory_girl/factory.rb
  3. +29 −4 test/factory_test.rb
  4. +8 −2 test/integration_test.rb
View
@@ -17,7 +17,7 @@
# Example:
# Factory(:user, :name => 'Joe')
def Factory (name, attrs = {})
Factory.create(name, attrs)
Factory.default_strategy(name, attrs)
end
if defined? Rails
@@ -48,6 +48,10 @@ def class_name #:nodoc
def build_class #:nodoc:
@build_class ||= class_for(class_name)
end
def default_strategy #:nodoc:
@options[:default_strategy] || :create
end
def initialize (name, options = {}) #:nodoc:
assert_valid_options(options)
@@ -170,7 +174,7 @@ def sequence (name, &block)
s = Sequence.new(&block)
add_attribute(name) { s.next }
end
# Generates and returns a Hash of attributes from this factory. Attributes
# can be individually overridden by passing in a Hash of attribute => value
# pairs.
@@ -230,7 +234,11 @@ def self.create (name, overrides = {})
# A mock object with generated attributes stubbed out (Object)
def self.stub (name, 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:
definition_file_paths.each do |path|
@@ -284,10 +292,17 @@ def attribute_defined? (name)
end
def assert_valid_options(options)
invalid_keys = options.keys - [:class, :parent]
invalid_keys = options.keys - [:class, :parent, :default_strategy]
unless invalid_keys == []
raise ArgumentError, "Unknown arguments: #{invalid_keys.inspect}"
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
# Based on ActiveSupport's underscore inflector
View
@@ -46,13 +46,17 @@ class FactoryTest < Test::Unit::TestCase
should "have a build class" do
assert_equal @class, @factory.build_class
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
assert_raise(Factory::AttributeDefinitionError) do
2.times { @factory.add_attribute :first_name }
end
end
should "add a static attribute when an attribute is defined with a value" do
attribute = mock('attribute', :name => :name)
Factory::Attribute::Static.
@@ -238,7 +242,7 @@ class FactoryTest < Test::Unit::TestCase
assert_nil @result[:test]
end
end
should "guess the build class from the factory name" do
assert_equal User, @factory.build_class
end
@@ -362,8 +366,18 @@ class FactoryTest < Test::Unit::TestCase
returns('result')
assert_equal 'result', Factory.stub(@name, :attr => 'value')
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.
expects(:run).
with(Factory::Proxy::Create, :attr => 'value').
@@ -384,7 +398,7 @@ class FactoryTest < Test::Unit::TestCase
end
end
context 'defining a factory using a parent attribute' do
context 'defining a factory with a parent parameter' do
setup do
@parent = Factory.define :object do |f|
f.name 'Name'
@@ -422,6 +436,17 @@ class FactoryTest < Test::Unit::TestCase
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)
context "in a directory with #{files.to_sentence}" do
setup do
View
@@ -10,7 +10,7 @@ def setup
f.email {|a| "#{a.first_name}.#{a.last_name}@example.com".downcase }
end
Factory.define Post do |f|
Factory.define Post, :default_strategy => :attributes_for do |f|
f.name 'Test Post'
f.association :author, :factory => :user
end
@@ -131,7 +131,7 @@ def teardown
end
end
context "an instance generated by a factory with a custom class name" do
setup do
@@ -225,5 +225,11 @@ def teardown
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

0 comments on commit 1e1c22d

Please sign in to comment.