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:
|
||||
# Factory(:user, :name => 'Joe')
|
||||
def Factory (name, attrs = {})
|
||||
Factory.create(name, attrs)
|
||||
Factory.default_strategy(name, attrs)
|
||||
end
|
||||
|
||||
if defined? Rails
|
||||
|
|
|
@ -49,6 +49,10 @@ class Factory
|
|||
@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)
|
||||
@factory_name = factory_name_for(name)
|
||||
|
@ -232,6 +236,10 @@ class Factory
|
|||
factory_by_name(name).run(Proxy::Stub, overrides)
|
||||
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|
|
||||
require("#{path}.rb") if File.exists?("#{path}.rb")
|
||||
|
@ -284,10 +292,17 @@ class Factory
|
|||
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
|
||||
|
|
|
@ -47,6 +47,10 @@ factory = Factory.new(:post)
|
|||
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 }
|
||||
|
@ -363,7 +367,17 @@ factory = Factory.new(:post)
|
|||
assert_equal 'result', Factory.stub(@name, :attr => 'value')
|
||||
end
|
||||
|
||||
should "use Proxy::Create for the global Factory method" do
|
||||
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 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 @@ factory = Factory.new(:post)
|
|||
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 @@ factory = Factory.new(:post)
|
|||
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
|
||||
|
|
|
@ -10,7 +10,7 @@ class IntegrationTest < Test::Unit::TestCase
|
|||
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
|
||||
|
@ -226,4 +226,10 @@ class IntegrationTest < Test::Unit::TestCase
|
|||
|
||||
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
|
||||
|
|
Loading…
Reference in a new issue