2010-06-10 13:37:51 -04:00
|
|
|
require 'spec_helper'
|
2009-04-11 11:27:23 -04:00
|
|
|
|
2010-06-24 09:45:57 -04:00
|
|
|
describe FactoryGirl::Sequence do
|
2010-08-12 22:42:18 -04:00
|
|
|
describe "a basic sequence" do
|
2011-08-13 01:03:12 -04:00
|
|
|
let(:name) { :test }
|
2013-12-14 22:33:15 -05:00
|
|
|
subject { FactoryGirl::Sequence.new(name) { |n| "=#{n}" } }
|
2011-05-19 10:56:45 -04:00
|
|
|
|
2013-01-18 13:27:57 -05:00
|
|
|
its(:name) { should eq name }
|
|
|
|
its(:names) { should eq [name] }
|
|
|
|
its(:next) { should eq "=1" }
|
2009-04-11 11:27:23 -04:00
|
|
|
|
2011-08-13 01:03:12 -04:00
|
|
|
describe "when incrementing" do
|
|
|
|
before { subject.next }
|
2013-01-18 13:27:57 -05:00
|
|
|
its(:next) { should eq "=2" }
|
2009-04-11 11:27:23 -04:00
|
|
|
end
|
|
|
|
end
|
2011-01-25 17:55:40 -05:00
|
|
|
|
2010-08-12 22:42:18 -04:00
|
|
|
describe "a custom sequence" do
|
2013-12-14 22:33:15 -05:00
|
|
|
subject { FactoryGirl::Sequence.new(:name, "A") { |n| "=#{n}" } }
|
2013-01-18 13:27:57 -05:00
|
|
|
its(:next) { should eq "=A" }
|
2010-08-12 22:42:18 -04:00
|
|
|
|
2011-08-13 01:03:12 -04:00
|
|
|
describe "when incrementing" do
|
|
|
|
before { subject.next }
|
2013-01-18 13:27:57 -05:00
|
|
|
its(:next) { should eq "=B" }
|
2010-08-12 22:42:18 -04:00
|
|
|
end
|
|
|
|
end
|
2011-01-27 16:25:31 -05:00
|
|
|
|
2012-04-01 08:20:19 -04:00
|
|
|
describe "a sequence with aliases using default value" do
|
2012-04-20 23:06:48 -04:00
|
|
|
subject { FactoryGirl::Sequence.new(:test, aliases: [:alias, :other]) { |n| "=#{n}" } }
|
2013-01-18 13:27:57 -05:00
|
|
|
its(:next) { should eq "=1" }
|
|
|
|
its(:names) { should eq [:test, :alias, :other] }
|
2012-04-01 08:20:19 -04:00
|
|
|
|
|
|
|
describe "when incrementing" do
|
|
|
|
before { subject.next }
|
2013-01-18 13:27:57 -05:00
|
|
|
its(:next) { should eq "=2" }
|
2012-04-01 08:20:19 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "a sequence with custom value and aliases" do
|
2012-04-06 14:41:13 -04:00
|
|
|
subject { FactoryGirl::Sequence.new(:test, 3, aliases: [:alias, :other]) { |n| "=#{n}" } }
|
2013-01-18 13:27:57 -05:00
|
|
|
its(:next) { should eq "=3" }
|
2012-03-29 10:21:55 -04:00
|
|
|
|
|
|
|
describe "when incrementing" do
|
|
|
|
before { subject.next }
|
2013-01-18 13:27:57 -05:00
|
|
|
its(:next) { should eq "=4" }
|
2012-03-29 10:21:55 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-01-27 16:25:31 -05:00
|
|
|
describe "a basic sequence without a block" do
|
2011-08-13 01:03:12 -04:00
|
|
|
subject { FactoryGirl::Sequence.new(:name) }
|
2013-01-18 13:27:57 -05:00
|
|
|
its(:next) { should eq 1 }
|
2011-01-25 17:55:40 -05:00
|
|
|
|
2011-08-13 01:03:12 -04:00
|
|
|
describe "when incrementing" do
|
|
|
|
before { subject.next }
|
2013-01-18 13:27:57 -05:00
|
|
|
its(:next) { should eq 2 }
|
2011-01-27 16:25:31 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "a custom sequence without a block" do
|
2011-08-13 01:03:12 -04:00
|
|
|
subject { FactoryGirl::Sequence.new(:name, "A") }
|
2013-01-18 13:27:57 -05:00
|
|
|
its(:next) { should eq "A" }
|
2011-01-27 16:25:31 -05:00
|
|
|
|
2011-08-13 01:03:12 -04:00
|
|
|
describe "when incrementing" do
|
|
|
|
before { subject.next }
|
2013-01-18 13:27:57 -05:00
|
|
|
its(:next) { should eq "B" }
|
2011-01-27 16:25:31 -05:00
|
|
|
end
|
|
|
|
end
|
2012-05-13 01:54:41 -04:00
|
|
|
|
|
|
|
describe "iterating over items in an enumerator" do
|
2013-12-14 22:33:15 -05:00
|
|
|
subject { FactoryGirl::Sequence.new(:name, %w[foo bar].to_enum) { |n| "=#{n}" } }
|
2012-05-13 01:54:41 -04:00
|
|
|
|
|
|
|
it "navigates to the next items until no items remain" do
|
2013-01-18 13:27:57 -05:00
|
|
|
expect(subject.next).to eq "=foo"
|
|
|
|
expect(subject.next).to eq "=bar"
|
2012-05-13 01:54:41 -04:00
|
|
|
expect { subject.next }.to raise_error(StopIteration)
|
|
|
|
end
|
|
|
|
end
|
2013-01-11 12:05:17 -05:00
|
|
|
|
|
|
|
describe "a custom sequence and scope" do
|
2013-12-14 22:33:15 -05:00
|
|
|
subject { FactoryGirl::Sequence.new(:name, 'A') { |n| "=#{n}#{foo}" } }
|
2017-06-01 12:54:02 -04:00
|
|
|
let(:scope) { double("scope", foo: "attribute") }
|
2013-01-11 12:05:17 -05:00
|
|
|
|
|
|
|
it 'increments within the correct scope' do
|
2013-01-18 13:27:57 -05:00
|
|
|
expect(subject.next(scope)).to eq '=Aattribute'
|
2013-01-11 12:05:17 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
describe 'when incrementing' do
|
|
|
|
before { subject.next(scope) }
|
|
|
|
|
|
|
|
it 'increments within the correct scope' do
|
2013-01-18 13:27:57 -05:00
|
|
|
expect(subject.next(scope)).to eq '=Battribute'
|
2013-01-11 12:05:17 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2009-04-11 11:27:23 -04:00
|
|
|
end
|