我正在尝试 create_list in factory_bot rails 限制字符串数组中的序列
I am trying to create_list in factory_bot rails limiting the sequence from an array of strings
从直接的 ruby 代码我试图在我的工厂规范中实现一个序列来做类似于这个 ruby 代码块的事情
domains = %w(gmail.com yahoo.com msn.com)
d = []
100.times do |n|
d << domains[n % domains.length]
end
puts d // this will print restricted strings from the array 100 times
这就是我试图通过 rspec 中的序列实现的目的,以测试我的代码在数据库中创建 100 条记录后是否正常工作,这些记录已经包含在模型中
这是我的工厂
trait :random do
sequence(:local_government_area) do |l|
lga = %w(zuru fakai sakaba)
lga_to_save = []
100.times do |n|
lga_to_save << lga[n % lga.length]
end
puts lga_to_save
puts l
local_government_area { lga_to_save }
end
end
end
它在这里被调用
let(:hundred_students) { create_list(:biodata, 100, :random) }
我知道我的代码不是关闭的,因为我一直在调试,但我需要知道如何将 create_list
限制为 strings
的 array
对于 trait
您的序列代码不必担心记录的数量,因为 create_list
已经这样做了。试试这个:
trait :random do
sequence(:local_government_area) do |n|
lga = %w(zuru fakai sakaba)
lga[n % lga.length]
end
end
然后:
let(:hundred_students) { create_list(:biodata, 100, :random) }
从直接的 ruby 代码我试图在我的工厂规范中实现一个序列来做类似于这个 ruby 代码块的事情
domains = %w(gmail.com yahoo.com msn.com)
d = []
100.times do |n|
d << domains[n % domains.length]
end
puts d // this will print restricted strings from the array 100 times
这就是我试图通过 rspec 中的序列实现的目的,以测试我的代码在数据库中创建 100 条记录后是否正常工作,这些记录已经包含在模型中
这是我的工厂
trait :random do
sequence(:local_government_area) do |l|
lga = %w(zuru fakai sakaba)
lga_to_save = []
100.times do |n|
lga_to_save << lga[n % lga.length]
end
puts lga_to_save
puts l
local_government_area { lga_to_save }
end
end
end
它在这里被调用
let(:hundred_students) { create_list(:biodata, 100, :random) }
我知道我的代码不是关闭的,因为我一直在调试,但我需要知道如何将 create_list
限制为 strings
的 array
对于 trait
您的序列代码不必担心记录的数量,因为 create_list
已经这样做了。试试这个:
trait :random do
sequence(:local_government_area) do |n|
lga = %w(zuru fakai sakaba)
lga[n % lga.length]
end
end
然后:
let(:hundred_students) { create_list(:biodata, 100, :random) }