mirror of
https://github.com/thoughtbot/factory_bot.git
synced 2022-11-09 11:43:51 -05:00
Move DisallowsDuplicatesRegistry to a decorator
This commit is contained in:
parent
9bc8e50b44
commit
e8d32b9254
5 changed files with 21 additions and 50 deletions
|
@ -12,7 +12,6 @@ require 'factory_girl/strategy/create'
|
|||
require 'factory_girl/strategy/attributes_for'
|
||||
require 'factory_girl/strategy/stub'
|
||||
require 'factory_girl/strategy/null'
|
||||
require 'factory_girl/disallows_duplicates_registry'
|
||||
require 'factory_girl/registry'
|
||||
require 'factory_girl/null_factory'
|
||||
require 'factory_girl/null_object'
|
||||
|
@ -39,6 +38,7 @@ require 'factory_girl/find_definitions'
|
|||
require 'factory_girl/reload'
|
||||
require 'factory_girl/decorator'
|
||||
require 'factory_girl/decorator/attribute_hash'
|
||||
require 'factory_girl/decorator/disallows_duplicates_registry'
|
||||
require 'factory_girl/decorator/invocation_tracker'
|
||||
require 'factory_girl/decorator/invocation_ignorer'
|
||||
require 'factory_girl/version'
|
||||
|
|
|
@ -6,9 +6,9 @@ module FactoryGirl
|
|||
attr_accessor :duplicate_attribute_assignment_from_initialize_with
|
||||
|
||||
def initialize
|
||||
@factories = DisallowsDuplicatesRegistry.new(Registry.new('Factory'))
|
||||
@sequences = DisallowsDuplicatesRegistry.new(Registry.new('Sequence'))
|
||||
@traits = DisallowsDuplicatesRegistry.new(Registry.new('Trait'))
|
||||
@factories = Decorator::DisallowsDuplicatesRegistry.new(Registry.new('Factory'))
|
||||
@sequences = Decorator::DisallowsDuplicatesRegistry.new(Registry.new('Sequence'))
|
||||
@traits = Decorator::DisallowsDuplicatesRegistry.new(Registry.new('Trait'))
|
||||
@strategies = Registry.new('Strategy')
|
||||
@callback_names = Set.new
|
||||
@definition = Definition.new
|
||||
|
|
13
lib/factory_girl/decorator/disallows_duplicates_registry.rb
Normal file
13
lib/factory_girl/decorator/disallows_duplicates_registry.rb
Normal file
|
@ -0,0 +1,13 @@
|
|||
module FactoryGirl
|
||||
class Decorator
|
||||
class DisallowsDuplicatesRegistry < Decorator
|
||||
def register(name, item)
|
||||
if registered?(name)
|
||||
raise DuplicateDefinitionError, "#{@component.name} already registered: #{name}"
|
||||
else
|
||||
@component.register(name, item)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,17 +0,0 @@
|
|||
module FactoryGirl
|
||||
class DisallowsDuplicatesRegistry
|
||||
def initialize(component)
|
||||
@component = component
|
||||
end
|
||||
|
||||
delegate :clear, :each, :find, :[], :registered?, to: :@component
|
||||
|
||||
def register(name, item)
|
||||
if registered?(name)
|
||||
raise DuplicateDefinitionError, "#{@component.name} already registered: #{name}"
|
||||
else
|
||||
@component.register(name, item)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,35 +1,9 @@
|
|||
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) }
|
||||
describe FactoryGirl::Decorator::DisallowsDuplicatesRegistry do
|
||||
let(:registry) { stub("registry", name: 'Great thing', register: 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
|
||||
subject { described_class.new(registry) }
|
||||
|
||||
it "delegates #register to the registry when not registered" do
|
||||
registry.stubs(registered?: false)
|
||||
|
@ -38,6 +12,7 @@ describe FactoryGirl::DisallowsDuplicatesRegistry do
|
|||
end
|
||||
|
||||
it "raises when attempting to #register a previously registered strategy" do
|
||||
registry.stubs(registered?: true)
|
||||
expect { subject.register(:same_name, {}) }.
|
||||
to raise_error(FactoryGirl::DuplicateDefinitionError, "Great thing already registered: same_name")
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue