mirror of
https://github.com/thoughtbot/factory_bot.git
synced 2022-11-09 11:43:51 -05:00
Moved attribute validation into the Attribute class
This commit is contained in:
parent
03fa0d1180
commit
38bc8965db
4 changed files with 67 additions and 64 deletions
|
@ -1,12 +1,28 @@
|
|||
class Factory
|
||||
|
||||
class AttributeDefinitionError < RuntimeError
|
||||
end
|
||||
|
||||
class Attribute #:nodoc:
|
||||
|
||||
attr_reader :name
|
||||
attr_writer :static_value, :lazy_block
|
||||
|
||||
def initialize (name)
|
||||
def initialize (name, static_value, lazy_block)
|
||||
name = name.to_sym
|
||||
|
||||
if name.to_s =~ /=$/
|
||||
raise AttributeDefinitionError,
|
||||
"factory_girl uses 'f.#{name.to_s.chop} value' syntax " +
|
||||
"rather than 'f.#{name} = value'"
|
||||
end
|
||||
|
||||
unless static_value.nil? || lazy_block.nil?
|
||||
raise AttributeDefinitionError, "Both value and block given"
|
||||
end
|
||||
|
||||
@name = name
|
||||
@static_value = static_value
|
||||
@lazy_block = lazy_block
|
||||
end
|
||||
|
||||
def value (proxy)
|
||||
|
|
|
@ -1,8 +1,5 @@
|
|||
class Factory
|
||||
|
||||
class AttributeDefinitionError < RuntimeError
|
||||
end
|
||||
|
||||
cattr_accessor :factories, :sequences #:nodoc:
|
||||
self.factories = {}
|
||||
self.sequences = {}
|
||||
|
@ -93,25 +90,8 @@ class Factory
|
|||
# value: (Object)
|
||||
# If no block is given, this value will be used for this attribute.
|
||||
def add_attribute (name, value = nil, &block)
|
||||
name = name.to_sym
|
||||
|
||||
if name.to_s =~ /=$/
|
||||
raise AttributeDefinitionError,
|
||||
"factory_girl uses 'f.#{name.to_s.chop} #{value}' syntax " +
|
||||
"rather than 'f.#{name} #{value}'"
|
||||
end
|
||||
|
||||
attribute = Attribute.new(name)
|
||||
attribute = Attribute.new(name, value, block)
|
||||
@attributes << attribute
|
||||
|
||||
if block_given?
|
||||
unless value.nil?
|
||||
raise ArgumentError, "Both value and block given"
|
||||
end
|
||||
attribute.lazy_block = block
|
||||
else
|
||||
attribute.static_value = value
|
||||
end
|
||||
end
|
||||
|
||||
# Calls add_attribute using the missing method name as the name of the
|
||||
|
|
|
@ -2,23 +2,28 @@ require(File.join(File.dirname(__FILE__), 'test_helper'))
|
|||
|
||||
class AttributeTest < Test::Unit::TestCase
|
||||
|
||||
def setup
|
||||
@proxy = mock('attribute-proxy')
|
||||
end
|
||||
|
||||
context "an attribute" do
|
||||
|
||||
setup do
|
||||
@name = :user
|
||||
@proxy = mock('attribute-proxy')
|
||||
@attr = Factory::Attribute.new(@name)
|
||||
@attr = Factory::Attribute.new(@name, 'test', nil)
|
||||
end
|
||||
|
||||
should "have a name" do
|
||||
assert_equal @name, @attr.name
|
||||
end
|
||||
|
||||
context "after setting a static value" do
|
||||
end
|
||||
|
||||
context "an attribute with a static value" do
|
||||
|
||||
setup do
|
||||
@value = 'test'
|
||||
@attr.static_value = @value
|
||||
@attr = Factory::Attribute.new(:user, @value, nil)
|
||||
end
|
||||
|
||||
should "return the value" do
|
||||
|
@ -27,10 +32,11 @@ class AttributeTest < Test::Unit::TestCase
|
|||
|
||||
end
|
||||
|
||||
context "after setting a lazy value" do
|
||||
context "an attribute with a lazy value" do
|
||||
|
||||
setup do
|
||||
@attr.lazy_block = lambda { 'value' }
|
||||
@block = lambda { 'value' }
|
||||
@attr = Factory::Attribute.new(:user, nil, @block)
|
||||
end
|
||||
|
||||
should "call the block to return a value" do
|
||||
|
@ -38,12 +44,27 @@ class AttributeTest < Test::Unit::TestCase
|
|||
end
|
||||
|
||||
should "yield the attribute proxy to the block" do
|
||||
@attr.lazy_block = lambda {|a| a }
|
||||
@block = lambda {|a| a }
|
||||
@attr = Factory::Attribute.new(:user, nil, @block)
|
||||
assert_equal @proxy, @attr.value(@proxy)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
should "raise an error when defining an attribute writer" do
|
||||
assert_raise Factory::AttributeDefinitionError do
|
||||
Factory::Attribute.new('test=', nil, nil)
|
||||
end
|
||||
end
|
||||
|
||||
should "not allow attributes to be added with both a value parameter and a block" do
|
||||
assert_raise(Factory::AttributeDefinitionError) do
|
||||
Factory::Attribute.new(:name, 'value', lambda {})
|
||||
end
|
||||
end
|
||||
|
||||
should "convert names to symbols" do
|
||||
assert_equal :name, Factory::Attribute.new('name', nil, nil).name
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
@ -53,14 +53,6 @@ class FactoryTest < Test::Unit::TestCase
|
|||
|
||||
end
|
||||
|
||||
should "raise an error when defining a factory when using attribute setters" do
|
||||
assert_raise Factory::AttributeDefinitionError do
|
||||
Factory.define(:user) do |f|
|
||||
f.name = 'test'
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context "defining a sequence" do
|
||||
|
||||
setup do
|
||||
|
@ -223,12 +215,6 @@ class FactoryTest < Test::Unit::TestCase
|
|||
assert_equal @value, @factory.attributes_for[@attr]
|
||||
end
|
||||
|
||||
should "not allow attributes to be added with both a value parameter and a block" do
|
||||
assert_raise(ArgumentError) do
|
||||
@factory.add_attribute(:name, 'value') {}
|
||||
end
|
||||
end
|
||||
|
||||
should "allow attributes to be added with strings as names" do
|
||||
@factory.add_attribute('name', 'value')
|
||||
assert_equal 'value', @factory.attributes_for[:name]
|
||||
|
|
Loading…
Reference in a new issue