Permalink
Browse files

Move proxy strategy checking to proxy

  • Loading branch information...
1 parent eb73dc1 commit a09ec68b4aeee848e85ea11b3cf4467deac919ec @joshuaclayton joshuaclayton committed Oct 20, 2011
Showing with 19 additions and 9 deletions.
  1. +1 −7 lib/factory_girl/factory.rb
  2. +6 −0 lib/factory_girl/proxy.rb
  3. +2 −2 spec/factory_girl/factory_spec.rb
  4. +10 −0 spec/factory_girl/proxy_spec.rb
@@ -168,18 +168,12 @@ def assert_valid_options(options)
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
@@ -75,5 +75,11 @@ def method_missing(method, *args, &block)
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
@@ -197,8 +197,8 @@
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
@@ -42,3 +42,13 @@
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

0 comments on commit a09ec68

Please sign in to comment.