mirror of
https://github.com/thoughtbot/factory_bot.git
synced 2022-11-09 11:43:51 -05:00
Defined method_missing on Factory so that attributes can be defined by calling the attribute name as a method
This commit is contained in:
parent
312bfa3528
commit
f6db21769d
2 changed files with 25 additions and 0 deletions
|
@ -69,6 +69,24 @@ class Factory
|
|||
end
|
||||
end
|
||||
|
||||
# Calls add_attribute using the missing method name as the name of the
|
||||
# attribute, so that:
|
||||
#
|
||||
# Factory.define :user do |f|
|
||||
# f.name 'Billy Idol'
|
||||
# end
|
||||
#
|
||||
# and:
|
||||
#
|
||||
# Factory.define :user do |f|
|
||||
# f.add_attribute :user, 'Billy Idol'
|
||||
# end
|
||||
#
|
||||
# are equivilent.
|
||||
def method_missing (name, *args, &block)
|
||||
add_attribute(name, *args, &block)
|
||||
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.
|
||||
|
|
|
@ -108,6 +108,13 @@ class FactoryTest < Test::Unit::TestCase
|
|||
|
||||
end
|
||||
|
||||
should "add an attribute using the method name when passed an undefined method" do
|
||||
@attr = :first_name
|
||||
@value = 'Sugar'
|
||||
@factory.send(@attr, @value)
|
||||
assert_equal @value, @factory.attributes[@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') {}
|
||||
|
|
Loading…
Reference in a new issue