Permalink
Browse files

Attributes without arguments/blocks look for a sequence

  • Loading branch information...
1 parent 58ee31b commit 3e08d6f15943f89d2d056dda25bd0920b649948f @jferris jferris committed Oct 1, 2010
@@ -56,7 +56,11 @@ def add_attribute(name, value = nil, &block)
# are equivilent.
def method_missing(name, *args, &block)
if args.empty? && block.nil?
- association(name)
+ if sequence = FactoryGirl.sequences[name]
+ add_attribute(name) { sequence.next }
+ else
+ association(name)
+ end
else
add_attribute(name, *args, &block)
end
@@ -4,6 +4,10 @@
describe "integration" do
before do
FactoryGirl.define do
+ sequence :email do |n|
+ "somebody#{n}@example.com"
+ end
+
factory :user, :class => 'user' do
first_name 'Jimi'
last_name 'Hendrix'
@@ -26,8 +30,7 @@
last_name 'Stein'
admin true
sequence(:username) { |n| "username#{n}" }
- # TODO: add sugar for this
- email { Factory.next(:email) }
+ email
end
factory :sequence_abuser, :class => User do
@@ -54,10 +57,6 @@
name 'Supplier of Awesome'
association :owner, :factory => :user
end
-
- sequence :email do |n|
- "somebody#{n}@example.com"
- end
end
end
@@ -120,6 +120,16 @@
factory.attributes.should include(attr)
end
+ it "adds a sequence when passed an undefined method without arguments or a block" do
+ name = :airport
+ proxy = 'proxy'
+ FactoryGirl.sequences[name] = FactoryGirl::Sequence.new { |value| "expected" }
+ subject.send(name)
+ stub(proxy).set
+ factory.attributes.last.add_to(proxy)
+ proxy.should have_received.set(name, 'expected')
+ end
+
it "registers its factory for an alias" do
aliased_name = :guest
mock(FactoryGirl).register_factory(factory, :as => aliased_name)

0 comments on commit 3e08d6f

Please sign in to comment.