mirror of
https://github.com/thoughtbot/factory_bot.git
synced 2022-11-09 11:43:51 -05:00
Clean up Registry decorator
This commit is contained in:
parent
c8c1a801ed
commit
0dcd70cd14
8 changed files with 72 additions and 62 deletions
|
@ -8,7 +8,7 @@ require "factory_girl/strategy/create"
|
|||
require "factory_girl/strategy/attributes_for"
|
||||
require "factory_girl/strategy/stub"
|
||||
require "factory_girl/strategy/null"
|
||||
require 'factory_girl/strict_registry'
|
||||
require 'factory_girl/disallows_duplicates_registry'
|
||||
require 'factory_girl/registry'
|
||||
require 'factory_girl/null_factory'
|
||||
require 'factory_girl/null_object'
|
||||
|
@ -36,7 +36,7 @@ require 'factory_girl/version'
|
|||
|
||||
module FactoryGirl
|
||||
def self.factories
|
||||
@factories ||= StrictRegistry.new(Registry.new("Factory"))
|
||||
@factories ||= DisallowsDuplicatesRegistry.new(Registry.new("Factory"))
|
||||
end
|
||||
|
||||
def self.register_factory(factory)
|
||||
|
@ -51,7 +51,7 @@ module FactoryGirl
|
|||
end
|
||||
|
||||
def self.sequences
|
||||
@sequences ||= StrictRegistry.new(Registry.new("Sequence"))
|
||||
@sequences ||= DisallowsDuplicatesRegistry.new(Registry.new("Sequence"))
|
||||
end
|
||||
|
||||
def self.register_sequence(sequence)
|
||||
|
@ -66,7 +66,7 @@ module FactoryGirl
|
|||
end
|
||||
|
||||
def self.traits
|
||||
@traits ||= StrictRegistry.new(Registry.new("Trait"))
|
||||
@traits ||= DisallowsDuplicatesRegistry.new(Registry.new("Trait"))
|
||||
end
|
||||
|
||||
def self.register_trait(trait)
|
||||
|
|
|
@ -1,10 +1,10 @@
|
|||
module FactoryGirl
|
||||
class StrictRegistry
|
||||
class DisallowsDuplicatesRegistry
|
||||
def initialize(component)
|
||||
@component = component
|
||||
end
|
||||
|
||||
delegate :each, :registered?, :clear, to: :@component
|
||||
delegate :clear, :each, :find, :[], :registered?, to: :@component
|
||||
|
||||
def register(name, item)
|
||||
if registered?(name)
|
||||
|
@ -13,13 +13,5 @@ module FactoryGirl
|
|||
@component.register(name, item)
|
||||
end
|
||||
end
|
||||
|
||||
def find(name)
|
||||
if registered?(name)
|
||||
@component.find(name)
|
||||
else
|
||||
raise ArgumentError, "#{@component.name} not registered: #{name}"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -9,26 +9,30 @@ module FactoryGirl
|
|||
@items = {}
|
||||
end
|
||||
|
||||
def register(name, item)
|
||||
@items[name.to_sym] = item
|
||||
def clear
|
||||
@items.clear
|
||||
end
|
||||
|
||||
def find(name)
|
||||
@items[name.to_sym]
|
||||
end
|
||||
|
||||
alias :[] :find
|
||||
|
||||
def each(&block)
|
||||
@items.values.uniq.each(&block)
|
||||
end
|
||||
|
||||
def find(name)
|
||||
if registered?(name)
|
||||
@items[name.to_sym]
|
||||
else
|
||||
raise ArgumentError, "#{@name} not registered: #{name}"
|
||||
end
|
||||
end
|
||||
|
||||
alias :[] :find
|
||||
|
||||
def register(name, item)
|
||||
@items[name.to_sym] = item
|
||||
end
|
||||
|
||||
def registered?(name)
|
||||
@items.key?(name.to_sym)
|
||||
end
|
||||
|
||||
def clear
|
||||
@items.clear
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -19,8 +19,7 @@ module FactoryGirl
|
|||
end
|
||||
|
||||
def strategy_name_to_object
|
||||
FactoryGirl.strategy_by_name(@name_or_object) ||
|
||||
raise("unrecognized method #{@name_or_object}")
|
||||
FactoryGirl.strategy_by_name(@name_or_object)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
44
spec/factory_girl/disallows_duplicates_registry_spec.rb
Normal file
44
spec/factory_girl/disallows_duplicates_registry_spec.rb
Normal file
|
@ -0,0 +1,44 @@
|
|||
require "spec_helper"
|
||||
|
||||
describe FactoryGirl::DisallowsDuplicatesRegistry do
|
||||
let(:registry) { stub("registry", name: "Great thing", register: true, find: true, each: true, clear: true, registered?: true, :[] => true) }
|
||||
|
||||
subject { FactoryGirl::DisallowsDuplicatesRegistry.new(registry) }
|
||||
|
||||
it "delegates #each to the registry" do
|
||||
block = -> {}
|
||||
subject.each(block)
|
||||
registry.should have_received(:each).with(block)
|
||||
end
|
||||
|
||||
it "delegates #registered? to the registry" do
|
||||
subject.registered?(:great_name)
|
||||
registry.should have_received(:registered?).with(:great_name)
|
||||
end
|
||||
|
||||
it "delegates #clear to the registry" do
|
||||
subject.clear
|
||||
registry.should have_received(:clear)
|
||||
end
|
||||
|
||||
it "delegates #find to the registry" do
|
||||
subject.find(:awesome)
|
||||
registry.should have_received(:find).with(:awesome)
|
||||
end
|
||||
|
||||
it "delegates #[] to the registry" do
|
||||
subject[:awesome]
|
||||
registry.should have_received(:[]).with(:awesome)
|
||||
end
|
||||
|
||||
it "delegates #register to the registry when not registered" do
|
||||
registry.stubs(registered?: false)
|
||||
subject.register(:awesome, {})
|
||||
registry.should have_received(:register).with(:awesome, {})
|
||||
end
|
||||
|
||||
it "raises when attempting to #register a previously registered strategy" do
|
||||
expect { subject.register(:same_name, {}) }.
|
||||
to raise_error(FactoryGirl::DuplicateDefinitionError, "Great thing already registered: same_name")
|
||||
end
|
||||
end
|
|
@ -18,6 +18,10 @@ describe FactoryGirl::Registry do
|
|||
subject[:object_name].should == registered_object
|
||||
end
|
||||
|
||||
it "raises when an object cannot be found" do
|
||||
expect { subject.find(:object_name) }.to raise_error(ArgumentError, "Great thing not registered: object_name")
|
||||
end
|
||||
|
||||
it "adds and returns the object registered" do
|
||||
subject.register(:object_name, registered_object).should == registered_object
|
||||
end
|
||||
|
|
|
@ -18,6 +18,6 @@ describe FactoryGirl::StrategyCalculator do
|
|||
end
|
||||
|
||||
it "raises when passing a bogus strategy" do
|
||||
expect { FactoryGirl::StrategyCalculator.new(:bogus_strategy).strategy }.to raise_error "unrecognized method bogus_strategy"
|
||||
expect { FactoryGirl::StrategyCalculator.new(:bogus_strategy).strategy }.to raise_error(ArgumentError, /bogus_strategy/)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,33 +0,0 @@
|
|||
require "spec_helper"
|
||||
|
||||
describe FactoryGirl::StrictRegistry do
|
||||
let(:registry) { stub("registry", name: "Great thing", register: true, find: true, each: true, registered?: true, clear: true) }
|
||||
|
||||
subject { FactoryGirl::StrictRegistry.new(registry) }
|
||||
|
||||
it "delegates #each to the registry" do
|
||||
block = -> {}
|
||||
subject.each(block)
|
||||
registry.should have_received(:each).with(block)
|
||||
end
|
||||
|
||||
it "delegates #registered? to the registry" do
|
||||
subject.registered?(:great_name)
|
||||
registry.should have_received(:registered?).with(:great_name)
|
||||
end
|
||||
|
||||
it "delegates #clear to the registry" do
|
||||
subject.clear
|
||||
registry.should have_received(:clear)
|
||||
end
|
||||
|
||||
it "raises when trying to find an unregistered object" do
|
||||
registry.stubs(registered?: false)
|
||||
expect { subject.find(:bogus) }.to raise_error(ArgumentError, "Great thing not registered: bogus")
|
||||
end
|
||||
|
||||
it "doesn't allow a duplicate name" do
|
||||
expect { 2.times { subject.register(:same_name, {}) } }.
|
||||
to raise_error(FactoryGirl::DuplicateDefinitionError, "Great thing already registered: same_name")
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue