Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

sequence can now have multiple aliases

  • Loading branch information...
commit f387e3865f0b7db45c5bc405cdbcfda6876500c0 1 parent 055c976
Kristian Mandrup kristianmandrup authored joshuaclayton committed
18 lib/factory_girl/sequence.rb
View
@@ -3,12 +3,14 @@ module FactoryGirl
# Sequences are defined using sequence within a FactoryGirl.define block.
# Sequence values are generated using next.
class Sequence
- attr_reader :name
+ attr_reader :name, :names, :value
- def initialize(name, value = 1, &proc) #:nodoc:
- @name = name
- @proc = proc
- @value = value
+ def initialize(*names, &proc) #:nodoc:
+ names.flatten!
+ @value = value?(names.last) ? names.slice!(-1) : 1
+ @names = names
+ @name = names.first
+ @proc = proc
end
def next
@@ -17,8 +19,10 @@ def next
@value = @value.next
end
- def names
- [@name]
+ private
+
+ def value? item
+ item.is_a?(Numeric) || item.is_a?(String)
end
end
end
8 lib/factory_girl/syntax/default.rb
View
@@ -33,6 +33,14 @@ def sequence(name, start_value = 1, &block)
FactoryGirl.register_sequence(Sequence.new(name, start_value, &block))
end
+ # define aliased sequence
+ # - register multiple sequences with same logic but different identifying names
+ def sequences(*names, &block)
+ names.each do |name|
+ FactoryGirl.register_sequence(Sequence.new(names, &block))
+ end
+ end
+
def trait(name, &block)
FactoryGirl.register_trait(Trait.new(name, &block))
end
9 spec/factory_girl/registry_spec.rb
View
@@ -3,6 +3,8 @@
describe FactoryGirl::Registry do
let(:aliases) { [:thing, :widget] }
let(:sequence) { FactoryGirl::Sequence.new(:email) { |n| "somebody#{n}@example.com" } }
+ let(:sequences) { FactoryGirl::Sequence.new(:email, :sender, :receiver) { |n| "some_one_else#{n}@example.com" } }
+
let(:factory) { FactoryGirl::Factory.new(:object) }
let(:other_factory) { FactoryGirl::Factory.new(:string) }
let(:factory_with_aliases) { FactoryGirl::Factory.new(:string, aliases: aliases) }
@@ -61,6 +63,13 @@
subject.find(:email).should == sequence
end
+ it "registers a multi sequences" do
+ subject.add(sequences)
+ subject.find(:email).should == sequences
+ subject.find(:sender).should == sequences
+ subject.find(:receiver).should == sequences
+ end
+
it "doesn't allow a duplicate name" do
expect { 2.times { subject.add(factory) } }.
to raise_error(FactoryGirl::DuplicateDefinitionError, "Factory already registered: object")
17 spec/factory_girl/sequence_spec.rb
View
@@ -25,6 +25,23 @@
end
end
+ describe "a sequence with multiple names (aliases)" do
+ let(:name) { :test }
+ let(:value) { 3 }
+ let(:aliases) { [:alias, :other] }
+ subject { FactoryGirl::Sequence.new(name, :alias, :other, value) {|n| "=#{n}" } }
+
+ its(:name) { should == name }
+ its(:value) { should == value }
+ its(:names) { should == ([name] + aliases) }
+ its(:next) { should == "=3" }
+
+ describe "when incrementing" do
+ before { subject.next }
+ its(:next) { should == "=4" }
+ end
+ end
+
describe "a basic sequence without a block" do
subject { FactoryGirl::Sequence.new(:name) }
its(:next) { should == 1 }
Please sign in to comment.
Something went wrong with that request. Please try again.