diff --git a/lib/factory_bot/strategy_syntax_method_registrar.rb b/lib/factory_bot/strategy_syntax_method_registrar.rb index 5813c9d8..a70ca074 100644 --- a/lib/factory_bot/strategy_syntax_method_registrar.rb +++ b/lib/factory_bot/strategy_syntax_method_registrar.rb @@ -39,7 +39,7 @@ def define_list_strategy_method Array.new(amount) do |i| block_with_index = StrategySyntaxMethodRegistrar.with_index(block, i) - send(strategy_name, name, *traits_and_overrides, &block_with_index) + send(strategy_name, name, *evaluate_sequence_attributes(traits_and_overrides, i), &block_with_index) end end end diff --git a/lib/factory_bot/syntax/methods.rb b/lib/factory_bot/syntax/methods.rb index d03f63f2..cb773939 100644 --- a/lib/factory_bot/syntax/methods.rb +++ b/lib/factory_bot/syntax/methods.rb @@ -129,6 +129,18 @@ def generate_list(name, count) Internal.sequence_by_name(name).next end end + + def build_sequence(&block) + block + end + + def evaluate_sequence_attributes(traits_and_overrides, i) + traits_and_overrides.map do |attribute| + next attribute unless attribute.is_a?(Hash) + + attribute.transform_values { |value| value.respond_to?(:call) ? value.call(i) : value } + end + end end end end diff --git a/spec/acceptance/create_list_spec.rb b/spec/acceptance/create_list_spec.rb index 891c00f8..591cc9a0 100644 --- a/spec/acceptance/create_list_spec.rb +++ b/spec/acceptance/create_list_spec.rb @@ -38,6 +38,16 @@ end end + context "with sequencial attributes" do + subject { FactoryBot.create_list(:post, 20, title: FactoryBot.build_sequence{|n| "title_#{n}"}) } + + it "create sequencial attribute values" do + subject.each_with_index do |record, i| + expect(record.title).to eq "title_#{i}" + end + end + end + context "with a block" do subject do FactoryBot.create_list(:post, 20, title: "The Listing of the Block") do |post|