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/acceptance/sequence_spec.rb

73 lines
1.9 KiB
Ruby
Raw Normal View History

describe "sequences" do
include FactoryBot::Syntax::Methods
it "generates several values in the correct format" do
FactoryBot.define do
sequence :email do |n|
"somebody#{n}@example.com"
end
end
first_value = generate(:email)
another_value = generate(:email)
2013-01-18 13:27:57 -05:00
expect(first_value).to match /^somebody\d+@example\.com$/
expect(another_value).to match /^somebody\d+@example\.com$/
expect(first_value).not_to eq another_value
end
it "generates sequential numbers if no block is given" do
FactoryBot.define do
sequence :order
end
first_value = generate(:order)
another_value = generate(:order)
2013-01-18 13:27:57 -05:00
expect(first_value).to eq 1
expect(another_value).to eq 2
expect(first_value).not_to eq another_value
end
2012-04-06 14:41:13 -04:00
it "generates aliases for the sequence that reference the same block" do
FactoryBot.define do
sequence(:size, aliases: [:count, :length]) { |n| "called-#{n}" }
2012-04-06 14:41:13 -04:00
end
first_value = generate(:size)
second_value = generate(:count)
third_value = generate(:length)
2013-01-18 13:27:57 -05:00
expect(first_value).to eq "called-1"
expect(second_value).to eq "called-2"
expect(third_value).to eq "called-3"
2012-04-06 14:41:13 -04:00
end
it "generates aliases for the sequence that reference the same block and retains value" do
FactoryBot.define do
sequence(:size, "a", aliases: [:count, :length]) { |n| "called-#{n}" }
2012-04-06 14:41:13 -04:00
end
first_value = generate(:size)
second_value = generate(:count)
third_value = generate(:length)
2013-01-18 13:27:57 -05:00
expect(first_value).to eq "called-a"
expect(second_value).to eq "called-b"
expect(third_value).to eq "called-c"
2012-04-06 14:41:13 -04:00
end
it "generates few values of the sequence" do
FactoryBot.define do
sequence :email do |n|
"somebody#{n}@example.com"
end
end
values = generate_list(:email, 2)
expect(values.first).to eq("somebody1@example.com")
expect(values.second).to eq("somebody2@example.com")
end
end