Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/faker_maker/factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ def overridden_value?( attr, attr_override_values )
end

def value_for_attribute( instance, attr, attr_override_values, chaos: false )
if !attr.embedded_factories? && overridden_value?( attr, attr_override_values )
if overridden_value?( attr, attr_override_values ) && !attr_override_values[attr.name].is_a?( Hash )
attr_override_values[attr.name]
elsif attr.array?
[].tap do |a|
Expand Down
2 changes: 1 addition & 1 deletion lib/faker_maker/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module FakerMaker
VERSION = '5.0.1'
VERSION = '5.0.2'
end
63 changes: 63 additions & 0 deletions spec/faker_maker/factory_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,69 @@
end
end

describe 'overriding embedded factory attributes' do
it 'allows FakerMaker::OMIT to be passed as an override for an embedded factory attribute' do
embed = FakerMaker::Factory.new( :override_embed )
embed.attach_attribute( FakerMaker::Attribute.new( :street, proc { '123 High St' } ) )
embed.attach_attribute( FakerMaker::Attribute.new( :city, proc { 'Swansea' } ) )
FakerMaker.register_factory( embed )

factory = FakerMaker::Factory.new( :override_parent )
factory.attach_attribute( FakerMaker::Attribute.new( :name, proc { 'Alice' } ) )
factory.attach_attribute( FakerMaker::Attribute.new( :address, nil, factory: :override_embed ) )
FakerMaker.register_factory( factory )

fake = factory.build( attributes: { address: FakerMaker::OMIT } )
expect( fake.address ).to eq FakerMaker::OMIT
end

it 'omits the embedded factory attribute from JSON when overridden with FakerMaker::OMIT' do
embed = FakerMaker::Factory.new( :override_embed )
embed.attach_attribute( FakerMaker::Attribute.new( :street, proc { '123 High St' } ) )
embed.attach_attribute( FakerMaker::Attribute.new( :city, proc { 'Swansea' } ) )
FakerMaker.register_factory( embed )

factory = FakerMaker::Factory.new( :override_parent )
factory.attach_attribute( FakerMaker::Attribute.new( :name, proc { 'Alice' } ) )
factory.attach_attribute( FakerMaker::Attribute.new( :address, nil, factory: :override_embed ) )
FakerMaker.register_factory( factory )

fake = factory.build( attributes: { address: FakerMaker::OMIT } )
expect( fake.as_json ).not_to have_key 'address'
end

it 'allows nil to be passed as an override for an embedded factory attribute' do
embed = FakerMaker::Factory.new( :override_embed )
embed.attach_attribute( FakerMaker::Attribute.new( :street, proc { '123 High St' } ) )
embed.attach_attribute( FakerMaker::Attribute.new( :city, proc { 'Swansea' } ) )
FakerMaker.register_factory( embed )

factory = FakerMaker::Factory.new( :override_parent )
factory.attach_attribute( FakerMaker::Attribute.new( :name, proc { 'Alice' } ) )
factory.attach_attribute( FakerMaker::Attribute.new( :address, nil, factory: :override_embed ) )
FakerMaker.register_factory( factory )

fake = factory.build( attributes: { address: nil } )
expect( fake.address ).to be_nil
end

it 'still passes Hash overrides through to the embedded factory' do
embed = FakerMaker::Factory.new( :override_embed )
embed.attach_attribute( FakerMaker::Attribute.new( :street, proc { '123 High St' } ) )
embed.attach_attribute( FakerMaker::Attribute.new( :city, proc { 'Swansea' } ) )
FakerMaker.register_factory( embed )

factory = FakerMaker::Factory.new( :override_parent )
factory.attach_attribute( FakerMaker::Attribute.new( :name, proc { 'Alice' } ) )
factory.attach_attribute( FakerMaker::Attribute.new( :address, nil, factory: :override_embed ) )
FakerMaker.register_factory( factory )

fake = factory.build( attributes: { address: { street: '456 Low Rd' } } )
expect( fake.address.street ).to eq '456 Low Rd'
expect( fake.address.city ).to eq 'Swansea'
end
end

describe '#instance' do
it 'returns the instance' do
factory = FakerMaker::Factory.new( :factory )
Expand Down
Loading