From fa3a555c198e66830f24b61a68397cd1fc666d83 Mon Sep 17 00:00:00 2001 From: Joe Ferris Date: Mon, 13 Apr 2009 21:39:19 -0400 Subject: [PATCH] Added an exception when defining a sequence from a dynamic attribute block [#43 state:resolved] --- lib/factory_girl/attribute/dynamic.rb | 7 +++++-- lib/factory_girl/sequence.rb | 3 +++ spec/factory_girl/attribute/dynamic_spec.rb | 10 ++++++++++ spec/integration_spec.rb | 10 +++++++++- 4 files changed, 27 insertions(+), 3 deletions(-) diff --git a/lib/factory_girl/attribute/dynamic.rb b/lib/factory_girl/attribute/dynamic.rb index 2429576..717830a 100644 --- a/lib/factory_girl/attribute/dynamic.rb +++ b/lib/factory_girl/attribute/dynamic.rb @@ -2,14 +2,17 @@ class Factory class Attribute #:nodoc: class Dynamic < Attribute #:nodoc: - def initialize(name, block) super(name) @block = block end def add_to(proxy) - proxy.set(name, @block.call(proxy)) + value = @block.call(proxy) + if Factory::Sequence === value + raise SequenceAbuseError + end + proxy.set(name, value) end end diff --git a/lib/factory_girl/sequence.rb b/lib/factory_girl/sequence.rb index fc18fc4..a7c5b43 100644 --- a/lib/factory_girl/sequence.rb +++ b/lib/factory_girl/sequence.rb @@ -1,5 +1,8 @@ class Factory + # Raised when calling Factory.sequence from a dynamic attribute block + class SequenceAbuseError < StandardError; end + # Sequences are defined using Factory.sequence. Sequence values are generated # using next. class Sequence diff --git a/spec/factory_girl/attribute/dynamic_spec.rb b/spec/factory_girl/attribute/dynamic_spec.rb index e37371e..32ef54e 100644 --- a/spec/factory_girl/attribute/dynamic_spec.rb +++ b/spec/factory_girl/attribute/dynamic_spec.rb @@ -33,6 +33,16 @@ describe Factory::Attribute::Dynamic do }.should raise_error(Factory::AttributeDefinitionError) end + it "should raise an error when returning a sequence" do + stub(Factory).sequence { Factory::Sequence.new } + block = lambda { Factory.sequence(:email) } + attr = Factory::Attribute::Dynamic.new(:email, block) + proxy = stub!.set.subject + lambda { + attr.add_to(proxy) + }.should raise_error(Factory::SequenceAbuseError) + end + it "should convert names to symbols" do Factory::Attribute::Dynamic.new('name', nil).name.should == :name end diff --git a/spec/integration_spec.rb b/spec/integration_spec.rb index a0cba02..8ca5ca7 100644 --- a/spec/integration_spec.rb +++ b/spec/integration_spec.rb @@ -1,7 +1,6 @@ require File.expand_path(File.join(File.dirname(__FILE__), 'spec_helper')) describe "integration" do - before do Factory.define :user, :class => 'user' do |f| f.first_name 'Jimi' @@ -23,6 +22,10 @@ describe "integration" do f.email { Factory.next(:email) } end + Factory.define :sequence_abuser, :class => User do |f| + f.first_name { Factory.sequence(:email) } + end + Factory.define :guest, :parent => :user do |f| f.last_name 'Anonymous' f.username 'GuestUser' @@ -233,4 +236,9 @@ describe "integration" do end end + it "should raise Factory::SequenceAbuseError" do + lambda { + Factory(:sequence_abuser) + }.should raise_error(Factory::SequenceAbuseError) + end end