From bfb7b5903323f1b28a68e3e6010f42a815cb499d Mon Sep 17 00:00:00 2001 From: Mathieu Magnin Date: Tue, 17 Sep 2024 19:05:36 +0200 Subject: [PATCH] [#10813] When value_json of champ SIRET is nil do not crash --- app/models/address_proxy.rb | 2 +- spec/models/address_proxy_spec.rb | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 spec/models/address_proxy_spec.rb diff --git a/app/models/address_proxy.rb b/app/models/address_proxy.rb index efc3767f4..e8463b5d6 100644 --- a/app/models/address_proxy.rb +++ b/app/models/address_proxy.rb @@ -20,7 +20,7 @@ class AddressProxy end def initialize(champ) - @data = champ.value_json.with_indifferent_access + @data = champ.value_json&.with_indifferent_access || {} end end diff --git a/spec/models/address_proxy_spec.rb b/spec/models/address_proxy_spec.rb new file mode 100644 index 000000000..01c513d88 --- /dev/null +++ b/spec/models/address_proxy_spec.rb @@ -0,0 +1,26 @@ +# frozen_string_literal: true + +RSpec.describe AddressProxy, type: :model do + describe '#initialize' do + subject { AddressProxy.new(champ_or_etablissement) } + + context 'when champ_or_etablissement is an instance of Champ' do + let(:champ_or_etablissement) { Champ.new } + + context 'when value_json is nil' do + before { allow(champ_or_etablissement).to receive(:value_json).and_return(nil) } + + it do + expect(subject.street_address).to be_nil + expect(subject.city_name).to be_nil + expect(subject.postal_code).to be_nil + expect(subject.city_code).to be_nil + expect(subject.departement_name).to be_nil + expect(subject.departement_code).to be_nil + expect(subject.region_name).to be_nil + expect(subject.region_code).to be_nil + end + end + end + end +end