Skip to content

Commit

Permalink
Attributes without arguments/blocks look for a sequence
Browse files Browse the repository at this point in the history
  • Loading branch information
jferris committed Oct 1, 2010
1 parent 58ee31b commit 3e08d6f
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
6 changes: 5 additions & 1 deletion lib/factory_girl/definition_proxy.rb
Expand Up @@ -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
Expand Down
11 changes: 5 additions & 6 deletions spec/acceptance/acceptance_spec.rb
Expand Up @@ -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'
Expand All @@ -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
Expand All @@ -54,10 +57,6 @@
name 'Supplier of Awesome'
association :owner, :factory => :user
end

sequence :email do |n|
"somebody#{n}@example.com"
end
end
end

Expand Down
10 changes: 10 additions & 0 deletions spec/factory_girl/definition_proxy_spec.rb
Expand Up @@ -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)
Expand Down

0 comments on commit 3e08d6f

Please sign in to comment.