mirror of
https://github.com/thoughtbot/factory_bot.git
synced 2022-11-09 11:43:51 -05:00
Added an exception when defining a sequence from a dynamic attribute block [#43 state:resolved]
This commit is contained in:
parent
683aa43fab
commit
fa3a555c19
4 changed files with 27 additions and 3 deletions
|
@ -2,14 +2,17 @@ class Factory
|
||||||
class Attribute #:nodoc:
|
class Attribute #:nodoc:
|
||||||
|
|
||||||
class Dynamic < Attribute #:nodoc:
|
class Dynamic < Attribute #:nodoc:
|
||||||
|
|
||||||
def initialize(name, block)
|
def initialize(name, block)
|
||||||
super(name)
|
super(name)
|
||||||
@block = block
|
@block = block
|
||||||
end
|
end
|
||||||
|
|
||||||
def add_to(proxy)
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,8 @@
|
||||||
class Factory
|
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
|
# Sequences are defined using Factory.sequence. Sequence values are generated
|
||||||
# using next.
|
# using next.
|
||||||
class Sequence
|
class Sequence
|
||||||
|
|
|
@ -33,6 +33,16 @@ describe Factory::Attribute::Dynamic do
|
||||||
}.should raise_error(Factory::AttributeDefinitionError)
|
}.should raise_error(Factory::AttributeDefinitionError)
|
||||||
end
|
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
|
it "should convert names to symbols" do
|
||||||
Factory::Attribute::Dynamic.new('name', nil).name.should == :name
|
Factory::Attribute::Dynamic.new('name', nil).name.should == :name
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
require File.expand_path(File.join(File.dirname(__FILE__), 'spec_helper'))
|
require File.expand_path(File.join(File.dirname(__FILE__), 'spec_helper'))
|
||||||
|
|
||||||
describe "integration" do
|
describe "integration" do
|
||||||
|
|
||||||
before do
|
before do
|
||||||
Factory.define :user, :class => 'user' do |f|
|
Factory.define :user, :class => 'user' do |f|
|
||||||
f.first_name 'Jimi'
|
f.first_name 'Jimi'
|
||||||
|
@ -23,6 +22,10 @@ describe "integration" do
|
||||||
f.email { Factory.next(:email) }
|
f.email { Factory.next(:email) }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Factory.define :sequence_abuser, :class => User do |f|
|
||||||
|
f.first_name { Factory.sequence(:email) }
|
||||||
|
end
|
||||||
|
|
||||||
Factory.define :guest, :parent => :user do |f|
|
Factory.define :guest, :parent => :user do |f|
|
||||||
f.last_name 'Anonymous'
|
f.last_name 'Anonymous'
|
||||||
f.username 'GuestUser'
|
f.username 'GuestUser'
|
||||||
|
@ -233,4 +236,9 @@ describe "integration" do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "should raise Factory::SequenceAbuseError" do
|
||||||
|
lambda {
|
||||||
|
Factory(:sequence_abuser)
|
||||||
|
}.should raise_error(Factory::SequenceAbuseError)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue