diff --git a/lib/factory_girl/definition_proxy.rb b/lib/factory_girl/definition_proxy.rb index 78e3e05..7912d79 100644 --- a/lib/factory_girl/definition_proxy.rb +++ b/lib/factory_girl/definition_proxy.rb @@ -56,7 +56,11 @@ module FactoryGirl # 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 diff --git a/spec/acceptance/acceptance_spec.rb b/spec/acceptance/acceptance_spec.rb index 1c2e1ce..3d8a09d 100644 --- a/spec/acceptance/acceptance_spec.rb +++ b/spec/acceptance/acceptance_spec.rb @@ -4,6 +4,10 @@ require 'acceptance/acceptance_helper' 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 @@ describe "integration" do 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 @@ describe "integration" do name 'Supplier of Awesome' association :owner, :factory => :user end - - sequence :email do |n| - "somebody#{n}@example.com" - end end end diff --git a/spec/factory_girl/definition_proxy_spec.rb b/spec/factory_girl/definition_proxy_spec.rb index 38812f3..60577a4 100644 --- a/spec/factory_girl/definition_proxy_spec.rb +++ b/spec/factory_girl/definition_proxy_spec.rb @@ -120,6 +120,16 @@ describe FactoryGirl::DefinitionProxy do 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)