mirror of
https://github.com/thoughtbot/factory_bot.git
synced 2022-11-09 11:43:51 -05:00
#6 - attribute names can now be specified using strings or symbols
This commit is contained in:
parent
e582435d34
commit
ab0b57a93d
2 changed files with 13 additions and 0 deletions
|
@ -93,6 +93,7 @@ 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 block_given?
|
||||
unless value.nil?
|
||||
raise ArgumentError, "Both value and block given"
|
||||
|
@ -196,6 +197,7 @@ class Factory
|
|||
private
|
||||
|
||||
def build_attributes_hash (override, strategy)
|
||||
override = override.symbolize_keys
|
||||
result = @static_attributes.merge(override)
|
||||
@lazy_attribute_names.each do |name|
|
||||
proxy = AttributeProxy.new(self, name, strategy, result)
|
||||
|
|
|
@ -181,6 +181,11 @@ class FactoryTest < Test::Unit::TestCase
|
|||
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]
|
||||
end
|
||||
|
||||
context "when overriding generated attributes with a hash" do
|
||||
|
||||
setup do
|
||||
|
@ -199,6 +204,12 @@ class FactoryTest < Test::Unit::TestCase
|
|||
@factory.attributes_for(@hash)
|
||||
end
|
||||
|
||||
should "override a symbol parameter with a string parameter" do
|
||||
@factory.add_attribute(@attr, 'The price is wrong, Bob!')
|
||||
@hash = { @attr.to_s => @value }
|
||||
assert_equal @value, @factory.attributes_for(@hash)[@attr]
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
should "guess the build class from the factory name" do
|
||||
|
|
Loading…
Reference in a new issue