From 8a65569f102ac2593e7c73bd029d5072e4641af8 Mon Sep 17 00:00:00 2001 From: Tejas Bubane Date: Fri, 26 Aug 2016 18:10:56 +0530 Subject: [PATCH] Add error message when count is missing for create_list Closes #889 `create_list` was failing abruptly if the second argument was not count. Give a descriptive error message for this case. --- lib/factory_girl/strategy_syntax_method_registrar.rb | 4 ++++ spec/acceptance/create_list_spec.rb | 8 ++++++++ 2 files changed, 12 insertions(+) diff --git a/lib/factory_girl/strategy_syntax_method_registrar.rb b/lib/factory_girl/strategy_syntax_method_registrar.rb index 0fbccc4..9c9cffb 100644 --- a/lib/factory_girl/strategy_syntax_method_registrar.rb +++ b/lib/factory_girl/strategy_syntax_method_registrar.rb @@ -25,6 +25,10 @@ module FactoryGirl strategy_name = @strategy_name define_syntax_method("#{strategy_name}_list") do |name, amount, *traits_and_overrides, &block| + unless amount.respond_to?(:times) + raise ArgumentError, "count missing for #{strategy_name}_list" + end + amount.times.map { send(strategy_name, name, *traits_and_overrides, &block) } end end diff --git a/spec/acceptance/create_list_spec.rb b/spec/acceptance/create_list_spec.rb index 5d87fd9..362436f 100644 --- a/spec/acceptance/create_list_spec.rb +++ b/spec/acceptance/create_list_spec.rb @@ -53,6 +53,14 @@ describe "create multiple instances" do end end end + + context "without the count" do + subject { FactoryGirl.create_list(:post, title: "The Hunting of the Bear") } + + it "raise ArgumentError with the proper error message" do + expect { subject }.to raise_error(ArgumentError, /count missing/) + end + end end describe "multiple creates and transient attributes to dynamically build attribute lists" do