Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: thoughtbot/factory_girl
...
head fork: thoughtbot/factory_girl
  • 6 commits
  • 8 files changed
  • 0 commit comments
  • 2 contributors
View
29 GETTING_STARTED.md
@@ -401,6 +401,35 @@ factory :post do
end
```
+Sequences can also have aliases. The sequence aliases share the same counter:
+
+```ruby
+factory :user do
+ sequence(:email, 1000, aliases: [:sender, :receiver]) {|n| "person#{n}@example.com" }
+end
+
+# will increase value counter for :email which is shared by :sender and :receiver
+FactoryGirl.next(:sender)
+```
+
+Define aliases and use default value (1) for the counter
+
+```ruby
+factory :user do
+ sequence(:email, aliases: [:sender, :receiver]) {|n| "person#{n}@example.com" }
+end
+```
+
+Setting the value:
+
+```ruby
+factory :user do
+ sequence(:email, 'a', aliases: [:sender, :receiver]) {|n| "person#{n}@example.com" }
+end
+```
+
+The value just needs to support the `#next` method. Here the next value will be 'a', then 'b', etc.
+
Traits
------
View
4 lib/factory_girl/definition_proxy.rb
@@ -108,8 +108,8 @@ def method_missing(name, *args, &block)
# end
#
# Except that no globally available sequence will be defined.
- def sequence(name, start_value = 1, &block)
- sequence = Sequence.new(name, start_value, &block)
+ def sequence(name, *args, &block)
+ sequence = Sequence.new(name, *args, &block)
add_attribute(name) { sequence.next }
end
View
13 lib/factory_girl/sequence.rb
@@ -5,10 +5,13 @@ module FactoryGirl
class Sequence
attr_reader :name
- def initialize(name, value = 1, &proc) #:nodoc:
- @name = name
- @proc = proc
- @value = value
+ def initialize(name, *args, &proc) #:nodoc:
+ @name = name
+ @proc = proc
+
+ options = args.extract_options!
+ @value = args.first || 1
+ @aliases = options[:aliases] || []
end
def next
@@ -18,7 +21,7 @@ def next
end
def names
- [@name]
+ [@name] + @aliases
end
end
end
View
4 lib/factory_girl/syntax/default.rb
@@ -29,8 +29,8 @@ def factory(name, options = {}, &block)
end
end
- def sequence(name, start_value = 1, &block)
- FactoryGirl.register_sequence(Sequence.new(name, start_value, &block))
+ def sequence(name, *args, &block)
+ FactoryGirl.register_sequence(Sequence.new(name, *args, &block))
end
def trait(name, &block)
View
28 spec/acceptance/sequence_spec.rb
@@ -30,4 +30,32 @@
another_value.should == 2
first_value.should_not == another_value
end
+
+ it "generates aliases for the sequence that reference the same block" do
+ FactoryGirl.define do
+ sequence(:size, aliases: [:count, :length]) {|n| "called-#{n}" }
+ end
+
+ first_value = generate(:size)
+ second_value = generate(:count)
+ third_value = generate(:length)
+
+ first_value.should == "called-1"
+ second_value.should == "called-2"
+ third_value.should == "called-3"
+ end
+
+ it "generates aliases for the sequence that reference the same block and retains value" do
+ FactoryGirl.define do
+ sequence(:size, "a", aliases: [:count, :length]) {|n| "called-#{n}" }
+ end
+
+ first_value = generate(:size)
+ second_value = generate(:count)
+ third_value = generate(:length)
+
+ first_value.should == "called-a"
+ second_value.should == "called-b"
+ third_value.should == "called-c"
+ end
end
View
2  spec/factory_girl/definition_proxy_spec.rb
@@ -91,7 +91,7 @@
it "creates a new sequence starting at 1" do
proxy.sequence(:great)
- FactoryGirl::Sequence.should have_received(:new).with(:great, 1)
+ FactoryGirl::Sequence.should have_received(:new).with(:great)
end
it "creates a new sequence with an overridden starting vaue" do
View
33 spec/factory_girl/registry_spec.rb
@@ -2,7 +2,6 @@
describe FactoryGirl::Registry do
let(:aliases) { [:thing, :widget] }
- let(:sequence) { FactoryGirl::Sequence.new(:email) { |n| "somebody#{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) }
@@ -56,11 +55,6 @@
subject.to_a.should =~ [factory, factory_with_aliases]
end
- it "registers an sequence" do
- subject.add(sequence)
- subject.find(:email).should == sequence
- end
-
it "doesn't allow a duplicate name" do
expect { 2.times { subject.add(factory) } }.
to raise_error(FactoryGirl::DuplicateDefinitionError, "Factory already registered: object")
@@ -79,3 +73,30 @@
subject.count.should be_zero
end
end
+
+describe FactoryGirl::Registry, "with sequences" do
+ let(:aliases) { [:sender, :receiver] }
+ let(:sequence) { FactoryGirl::Sequence.new(:email) { |n| "somebody#{n}@example.com" } }
+ let(:sequence_with_aliases) { FactoryGirl::Sequence.new(:email, aliases: aliases) { |n| "someone_else#{n}@example.com" } }
+ let(:registry_name) { "Sequence" }
+
+ subject { FactoryGirl::Registry.new(registry_name) }
+
+ it "registers an sequence" do
+ subject.add(sequence)
+ subject.find(:email).should == sequence
+ end
+
+ it "registers a sequence with aliases" do
+ subject.add(sequence_with_aliases)
+
+ subject.find(:email).should == sequence_with_aliases
+ subject.find(:sender).should == sequence_with_aliases
+ subject.find(:receiver).should == sequence_with_aliases
+ end
+
+ it "doesn't allow a duplicate name" do
+ expect { 2.times { subject.add(sequence_with_aliases) } }.
+ to raise_error(FactoryGirl::DuplicateDefinitionError, "Sequence already registered: email")
+ end
+end
View
20 spec/factory_girl/sequence_spec.rb
@@ -25,6 +25,26 @@
end
end
+ describe "a sequence with aliases using default value" do
+ subject { FactoryGirl::Sequence.new(:test, aliases: [:alias, :other]) { |n| "=#{n}" } }
+ its(:next) { should == "=1" }
+
+ describe "when incrementing" do
+ before { subject.next }
+ its(:next) { should == "=2" }
+ end
+ end
+
+ describe "a sequence with custom value and aliases" do
+ subject { FactoryGirl::Sequence.new(:test, 3, aliases: [:alias, :other]) { |n| "=#{n}" } }
+ 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 }

No commit comments for this range

Something went wrong with that request. Please try again.