mirror of
https://github.com/thoughtbot/factory_bot.git
synced 2022-11-09 11:43:51 -05:00
Added default strategy parameter
This commit is contained in:
parent
517cc944b3
commit
1e1c22d06e
4 changed files with 56 additions and 10 deletions
|
@ -17,7 +17,7 @@ require 'factory_girl/aliases'
|
||||||
# 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
|
||||||
|
|
|
@ -48,6 +48,10 @@ class Factory
|
||||||
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)
|
||||||
|
@ -170,7 +174,7 @@ class Factory
|
||||||
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.
|
||||||
|
@ -230,7 +234,11 @@ class Factory
|
||||||
# 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|
|
||||||
|
@ -284,10 +292,17 @@ class Factory
|
||||||
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
|
||||||
|
|
|
@ -46,13 +46,17 @@ factory = Factory.new(:post)
|
||||||
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.
|
||||||
|
@ -238,7 +242,7 @@ factory = Factory.new(:post)
|
||||||
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
|
||||||
|
@ -362,8 +366,18 @@ factory = Factory.new(:post)
|
||||||
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').
|
||||||
|
@ -384,7 +398,7 @@ factory = Factory.new(:post)
|
||||||
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'
|
||||||
|
@ -422,6 +436,17 @@ factory = Factory.new(:post)
|
||||||
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
|
||||||
|
|
|
@ -10,7 +10,7 @@ class IntegrationTest < Test::Unit::TestCase
|
||||||
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
|
||||||
|
@ -131,7 +131,7 @@ class IntegrationTest < Test::Unit::TestCase
|
||||||
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
|
||||||
|
@ -225,5 +225,11 @@ class IntegrationTest < Test::Unit::TestCase
|
||||||
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
|
||||||
|
|
Loading…
Add table
Reference in a new issue