From 6c49c69cd8d388831f791a35fe0279fd2fde2371 Mon Sep 17 00:00:00 2001 From: thoughtbot Date: Tue, 22 Jul 2008 19:46:01 -0400 Subject: [PATCH] Raise AttributeDefinitionError when defining an attribute with f.attr=value rather than f.attr value; ticket #8 --- lib/factory_girl/factory.rb | 9 ++++++++- test/factory_test.rb | 8 ++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/factory_girl/factory.rb b/lib/factory_girl/factory.rb index 746066e..ecda4ee 100644 --- a/lib/factory_girl/factory.rb +++ b/lib/factory_girl/factory.rb @@ -1,5 +1,8 @@ class Factory - + + class AttributeDefinitionError < RuntimeError + end + cattr_accessor :factories, :sequences #:nodoc: self.factories = {} self.sequences = {} @@ -93,6 +96,10 @@ class Factory # value: (Object) # If no block is given, this value will be used for this attribute. 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? unless value.nil? raise ArgumentError, "Both value and block given" diff --git a/test/factory_test.rb b/test/factory_test.rb index 48cba1f..c1442e9 100644 --- a/test/factory_test.rb +++ b/test/factory_test.rb @@ -51,6 +51,14 @@ class FactoryTest < Test::Unit::TestCase 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