From a09ec68b4aeee848e85ea11b3cf4467deac919ec Mon Sep 17 00:00:00 2001 From: Joshua Clayton Date: Thu, 20 Oct 2011 16:21:50 -0400 Subject: [PATCH] Move proxy strategy checking to proxy --- lib/factory_girl/factory.rb | 8 +------- lib/factory_girl/proxy.rb | 6 ++++++ spec/factory_girl/factory_spec.rb | 4 ++-- spec/factory_girl/proxy_spec.rb | 10 ++++++++++ 4 files changed, 19 insertions(+), 9 deletions(-) diff --git a/lib/factory_girl/factory.rb b/lib/factory_girl/factory.rb index 0dd25d3..64aa94e 100644 --- a/lib/factory_girl/factory.rb +++ b/lib/factory_girl/factory.rb @@ -168,18 +168,12 @@ module FactoryGirl options.assert_valid_keys(:class, :parent, :default_strategy, :aliases, :traits) if options[:default_strategy] - assert_valid_strategy(options[:default_strategy]) + Proxy.ensure_strategy_exists!(options[:default_strategy]) $stderr.puts "DEPRECATION WARNING: default_strategy is deprecated." $stderr.puts "Override to_create if you need to prevent a call to #save!." end end - def assert_valid_strategy(strategy) - unless Proxy.const_defined? strategy.to_s.camelize - raise ArgumentError, "Unknown strategy: #{strategy}" - end - end - def trait_for(name) @defined_traits.detect {|trait| trait.name == name } end diff --git a/lib/factory_girl/proxy.rb b/lib/factory_girl/proxy.rb index 0e1e701..698e843 100644 --- a/lib/factory_girl/proxy.rb +++ b/lib/factory_girl/proxy.rb @@ -75,5 +75,11 @@ module FactoryGirl def result(to_create) raise NotImplementedError, "Strategies must return a result" end + + def self.ensure_strategy_exists!(strategy) + unless Proxy.const_defined? strategy.to_s.camelize + raise ArgumentError, "Unknown strategy: #{strategy}" + end + end end end diff --git a/spec/factory_girl/factory_spec.rb b/spec/factory_girl/factory_spec.rb index 7492643..954a05f 100644 --- a/spec/factory_girl/factory_spec.rb +++ b/spec/factory_girl/factory_spec.rb @@ -197,8 +197,8 @@ describe FactoryGirl::Factory do FactoryGirl.register_factory(factory_with_stub_strategy) end - it "raises an ArgumentError when trying to use a non-existent strategy" do - expect { factory_with_non_existent_strategy }.to raise_error(ArgumentError) + it "raises when trying to use a non-existent strategy" do + expect { factory_with_non_existent_strategy }.to raise_error end it "creates a new factory with a specified default strategy" do diff --git a/spec/factory_girl/proxy_spec.rb b/spec/factory_girl/proxy_spec.rb index 22a7c73..6cd6c60 100644 --- a/spec/factory_girl/proxy_spec.rb +++ b/spec/factory_girl/proxy_spec.rb @@ -42,3 +42,13 @@ describe FactoryGirl::Proxy, "when running callbacks" do callback_result.should == [:after_build_one] end end + +describe FactoryGirl::Proxy, ".ensure_strategy_exists!" do + it "raises when passed a nonexistent strategy" do + expect { FactoryGirl::Proxy.ensure_strategy_exists!(:nonexistent) }.to raise_error(ArgumentError, "Unknown strategy: nonexistent") + end + + it "doesn't raise when passed a valid strategy" do + expect { FactoryGirl::Proxy.ensure_strategy_exists!(:create) }.to_not raise_error + end +end