mirror of
https://github.com/thoughtbot/factory_bot.git
synced 2022-11-09 11:43:51 -05:00
An error is now raised if you try to define the same attribute twice
This commit is contained in:
parent
38bc8965db
commit
13d988cd98
2 changed files with 15 additions and 0 deletions
|
@ -91,6 +91,11 @@ class Factory
|
||||||
# If no block is given, this value will be used for this attribute.
|
# If no block is given, this value will be used for this attribute.
|
||||||
def add_attribute (name, value = nil, &block)
|
def add_attribute (name, value = nil, &block)
|
||||||
attribute = Attribute.new(name, value, block)
|
attribute = Attribute.new(name, value, block)
|
||||||
|
|
||||||
|
if attribute_defined?(attribute.name)
|
||||||
|
raise AttributeDefinitionError, "Attribute already defined: #{name}"
|
||||||
|
end
|
||||||
|
|
||||||
@attributes << attribute
|
@attributes << attribute
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -250,4 +255,8 @@ class Factory
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def attribute_defined? (name)
|
||||||
|
!@attributes.detect {|attr| attr.name == name }.nil?
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -91,6 +91,12 @@ class FactoryTest < Test::Unit::TestCase
|
||||||
assert_equal @class, @factory.build_class
|
assert_equal @class, @factory.build_class
|
||||||
end
|
end
|
||||||
|
|
||||||
|
should "not allow the same attribute to be added twice" do
|
||||||
|
assert_raise(Factory::AttributeDefinitionError) do
|
||||||
|
2.times { @factory.add_attribute @name }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
context "when adding an attribute with a value parameter" do
|
context "when adding an attribute with a value parameter" do
|
||||||
|
|
||||||
setup do
|
setup do
|
||||||
|
|
Loading…
Reference in a new issue