1
0
Fork 0
mirror of https://github.com/thoughtbot/factory_bot.git synced 2022-11-09 11:43:51 -05:00
thoughtbot--factory_bot/spec/factory_bot/sequence_spec.rb

129 lines
3.7 KiB
Ruby
Raw Normal View History

2017-09-30 23:06:14 -04:00
shared_examples "a sequence" do |options|
first_value = options[:first_value]
2017-09-30 23:06:14 -04:00
second_value = options[:second_value]
it "has a next value equal to its first value" do
expect(subject.next).to eq first_value
end
it "has a next value equal to the 2nd value after being incremented" do
subject.next
2017-09-30 23:06:14 -04:00
expect(subject.next).to eq second_value
2017-09-30 23:06:14 -04:00
end
it "has a next value equal to the 1st value after rewinding" do
subject.next
subject.rewind
2017-09-30 23:06:14 -04:00
expect(subject.next).to eq first_value
2017-09-30 23:06:14 -04:00
end
end
2009-04-11 11:27:23 -04:00
describe FactoryBot::Sequence do
describe "a basic sequence" do
let(:name) { :test }
subject { FactoryBot::Sequence.new(name) { |n| "=#{n}" } }
its(:name) { should eq name }
2013-01-18 13:27:57 -05:00
its(:names) { should eq [name] }
2009-04-11 11:27:23 -04:00
2017-09-30 23:06:14 -04:00
it_behaves_like "a sequence", first_value: "=1", second_value: "=2"
2009-04-11 11:27:23 -04:00
end
describe "a custom sequence" do
2017-09-30 23:06:14 -04:00
subject { FactoryBot::Sequence.new(:name, "A") { |n| "=#{n}" } }
2017-09-30 23:06:14 -04:00
it_behaves_like "a sequence", first_value: "=A", second_value: "=B"
end
describe "a sequence with aliases using default value" do
2017-09-30 23:06:14 -04:00
subject do
FactoryBot::Sequence.new(:test, aliases: [:alias, :other]) do |n|
"=#{n}"
end
end
it "has the expected names as its names" do
names = [:foo, :bar, :baz]
sequence = FactoryBot::Sequence.new(names.first, aliases: names.last(2)) {
"=#{n}"
}
expect(sequence.names).to eq names
end
2017-09-30 23:06:14 -04:00
it_behaves_like "a sequence", first_value: "=1", second_value: "=2"
end
describe "a sequence with custom value and aliases" do
2017-09-30 23:06:14 -04:00
subject do
FactoryBot::Sequence.new(:test, 3, aliases: [:alias, :other]) do |n|
"=#{n}"
end
2012-03-29 10:21:55 -04:00
end
2017-09-30 23:06:14 -04:00
it "has the expected names as its names" do
names = [:foo, :bar, :baz]
sequence = FactoryBot::Sequence.new(names.first, 3, aliases: names.last(2)) {
"=#{n}"
}
expect(sequence.names).to eq names
end
2017-09-30 23:06:14 -04:00
it_behaves_like "a sequence", first_value: "=3", second_value: "=4"
2012-03-29 10:21:55 -04:00
end
describe "a basic sequence without a block" do
2017-09-30 23:06:14 -04:00
subject { FactoryBot::Sequence.new(:name) }
2017-09-30 23:06:14 -04:00
it_behaves_like "a sequence", first_value: 1, second_value: 2
end
describe "a custom sequence without a block" do
2017-09-30 23:06:14 -04:00
subject { FactoryBot::Sequence.new(:name, "A") }
2017-09-30 23:06:14 -04:00
it_behaves_like "a sequence", first_value: "A", second_value: "B"
end
describe "iterating over items in an enumerator" do
2017-09-30 23:06:14 -04:00
subject do
FactoryBot::Sequence.new(:name, %w[foo bar].to_enum) { |n| "=#{n}" }
end
it "navigates to the next items until no items remain" do
sequence = FactoryBot::Sequence.new(:name, %w[foo bar].to_enum) { |n| "=#{n}" }
expect(sequence.next).to eq "=foo"
expect(sequence.next).to eq "=bar"
expect { sequence.next }.to raise_error(StopIteration)
end
2017-09-30 23:06:14 -04:00
it_behaves_like "a sequence", first_value: "=foo", second_value: "=bar"
end
it "a custom sequence and scope increments within the correct scope" do
sequence = FactoryBot::Sequence.new(:name, "A") { |n| "=#{n}#{foo}" }
scope = double("scope", foo: "attribute")
expect(sequence.next(scope)).to eq "=Aattribute"
end
it "a custom sequence and scope increments within the correct scope when incrementing" do
sequence = FactoryBot::Sequence.new(:name, "A") { |n| "=#{n}#{foo}" }
scope = double("scope", foo: "attribute")
sequence.next(scope)
expect(sequence.next(scope)).to eq "=Battribute"
end
2017-09-30 23:06:14 -04:00
it "a custom scope increments within the correct scope after rewinding" do
sequence = FactoryBot::Sequence.new(:name, "A") { |n| "=#{n}#{foo}" }
scope = double("scope", foo: "attribute")
sequence.next(scope)
sequence.rewind
2017-09-30 23:06:14 -04:00
expect(sequence.next(scope)).to eq "=Aattribute"
end
2009-04-11 11:27:23 -04:00
end