Raise AttributeDefinitionError when defining an attribute with f.attr=value rather than f.attr value; ticket #8

This commit is contained in:
thoughtbot 2008-07-22 19:46:01 -04:00
parent edd39c652c
commit 6c49c69cd8
2 changed files with 16 additions and 1 deletions

View File

@ -1,5 +1,8 @@
class Factory class Factory
class AttributeDefinitionError < RuntimeError
end
cattr_accessor :factories, :sequences #:nodoc: cattr_accessor :factories, :sequences #:nodoc:
self.factories = {} self.factories = {}
self.sequences = {} self.sequences = {}
@ -93,6 +96,10 @@ class Factory
# value: (Object) # value: (Object)
# 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)
if name.to_s =~ /=$/
raise AttributeDefinitionError, "factory_girl uses 'f.#{name.to_s.chop} #{value}' syntax rather than 'f.#{name} #{value}'"
end
if block_given? if block_given?
unless value.nil? unless value.nil?
raise ArgumentError, "Both value and block given" raise ArgumentError, "Both value and block given"

View File

@ -51,6 +51,14 @@ class FactoryTest < Test::Unit::TestCase
end end
end 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 context "defining a sequence" do