2010-11-12 15:58:25 -05:00
|
|
|
describe "sequences" do
|
2017-10-20 15:20:28 -04:00
|
|
|
include FactoryBot::Syntax::Methods
|
2011-05-19 10:56:45 -04:00
|
|
|
|
2010-11-12 15:58:25 -05:00
|
|
|
it "generates several values in the correct format" do
|
2017-10-20 15:20:28 -04:00
|
|
|
FactoryBot.define do
|
2010-11-12 15:58:25 -05:00
|
|
|
sequence :email do |n|
|
|
|
|
"somebody#{n}@example.com"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-05-19 10:56:45 -04:00
|
|
|
first_value = generate(:email)
|
|
|
|
another_value = generate(:email)
|
2010-11-12 15:58:25 -05:00
|
|
|
|
2020-06-05 15:15:18 -04:00
|
|
|
expect(first_value).to match(/^somebody\d+@example\.com$/)
|
|
|
|
expect(another_value).to match(/^somebody\d+@example\.com$/)
|
2013-01-18 13:27:57 -05:00
|
|
|
expect(first_value).not_to eq another_value
|
2010-11-12 15:58:25 -05:00
|
|
|
end
|
2011-01-25 17:55:40 -05:00
|
|
|
|
2011-01-27 16:25:31 -05:00
|
|
|
it "generates sequential numbers if no block is given" do
|
2017-10-20 15:20:28 -04:00
|
|
|
FactoryBot.define do
|
2011-01-27 16:25:31 -05:00
|
|
|
sequence :order
|
|
|
|
end
|
|
|
|
|
2011-05-19 10:56:45 -04:00
|
|
|
first_value = generate(:order)
|
|
|
|
another_value = generate(:order)
|
2010-11-12 15:58:25 -05:00
|
|
|
|
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
|
2011-01-27 16:25:31 -05:00
|
|
|
end
|
2012-04-06 14:41:13 -04:00
|
|
|
|
|
|
|
it "generates aliases for the sequence that reference the same block" do
|
2017-10-20 15:20:28 -04:00
|
|
|
FactoryBot.define do
|
2013-12-14 22:33:15 -05:00
|
|
|
sequence(:size, aliases: [:count, :length]) { |n| "called-#{n}" }
|
2012-04-06 14:41:13 -04:00
|
|
|
end
|
|
|
|
|
2020-06-05 15:15:18 -04:00
|
|
|
first_value = generate(:size)
|
2012-04-06 14:41:13 -04:00
|
|
|
second_value = generate(:count)
|
2020-06-05 15:15:18 -04:00
|
|
|
third_value = generate(:length)
|
2012-04-06 14:41:13 -04:00
|
|
|
|
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
|
2017-10-20 15:20:28 -04:00
|
|
|
FactoryBot.define do
|
2013-12-14 22:33:15 -05:00
|
|
|
sequence(:size, "a", aliases: [:count, :length]) { |n| "called-#{n}" }
|
2012-04-06 14:41:13 -04:00
|
|
|
end
|
|
|
|
|
2020-06-05 15:15:18 -04:00
|
|
|
first_value = generate(:size)
|
2012-04-06 14:41:13 -04:00
|
|
|
second_value = generate(:count)
|
2020-06-05 15:15:18 -04:00
|
|
|
third_value = generate(:length)
|
2012-04-06 14:41:13 -04:00
|
|
|
|
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
|
2016-12-16 05:18:31 -05:00
|
|
|
|
|
|
|
it "generates few values of the sequence" do
|
2017-10-20 15:20:28 -04:00
|
|
|
FactoryBot.define do
|
2016-12-16 05:18:31 -05:00
|
|
|
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
|
2011-01-27 16:25:31 -05:00
|
|
|
end
|