mirror of
https://github.com/thoughtbot/factory_bot.git
synced 2022-11-09 11:43:51 -05:00
Move proxy strategy checking to proxy
This commit is contained in:
parent
eb73dc1979
commit
a09ec68b4a
4 changed files with 19 additions and 9 deletions
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue