Skip to content

Commit f5fbc18

Browse files
Add hoc sequences in create_list
1 parent c0f2679 commit f5fbc18

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

lib/factory_bot/strategy_syntax_method_registrar.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def define_list_strategy_method
3939

4040
Array.new(amount) do |i|
4141
block_with_index = StrategySyntaxMethodRegistrar.with_index(block, i)
42-
send(strategy_name, name, *traits_and_overrides, &block_with_index)
42+
send(strategy_name, name, *evaluate_sequence_attributes(traits_and_overrides, i), &block_with_index)
4343
end
4444
end
4545
end

lib/factory_bot/syntax/methods.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,18 @@ def generate_list(name, count)
129129
Internal.sequence_by_name(name).next
130130
end
131131
end
132+
133+
def build_sequence(&block)
134+
block
135+
end
136+
137+
def evaluate_sequence_attributes(traits_and_overrides, i)
138+
traits_and_overrides.map do |attribute|
139+
next attribute unless attribute.is_a?(Hash)
140+
141+
attribute.transform_values { |value| value.respond_to?(:call) ? value.call(i) : value }
142+
end
143+
end
132144
end
133145
end
134146
end

spec/acceptance/create_list_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,16 @@
3838
end
3939
end
4040

41+
context "with sequencial attributes" do
42+
subject { FactoryBot.create_list(:post, 20, title: FactoryBot.build_sequence{|n| "title_#{n}"}) }
43+
44+
it "create sequencial attribute values" do
45+
subject.each_with_index do |record, i|
46+
expect(record.title).to eq "title_#{i}"
47+
end
48+
end
49+
end
50+
4151
context "with a block" do
4252
subject do
4353
FactoryBot.create_list(:post, 20, title: "The Listing of the Block") do |post|

0 commit comments

Comments
 (0)