# frozen_string_literal: true RSpec.describe TwilioBase::PhoneNumber do describe '.find_or_create' do before do ENV['COUNTRY_CODE'] = 'GB' end after do ENV['COUNTRY_CODE'] = nil end context 'phone number exists' do it 'does not create a new phone number' do number = FactoryBot.generate(:phone_number) phone_number = TwilioBase::Fake::IncomingPhoneNumber .new.create(phone_number: number) expect { described_class.find_or_create(phone_number.phone_number) } .not_to( change { TwilioBase::Fake::IncomingPhoneNumber.phone_numbers.count } ) end it 'returns the phone number' do number = FactoryBot.generate(:phone_number) phone_number = TwilioBase::Fake::IncomingPhoneNumber .new.create(phone_number: number) expect(described_class.find_or_create(phone_number.phone_number)) .to eq phone_number end end context 'phone number does not exist' do it 'does creates a new phone number' do expect { described_class.find_or_create(nil) } .to change { TwilioBase::Fake::IncomingPhoneNumber.phone_numbers.count }.by(1) end it 'returns the phone number' do expect(described_class.find_or_create(nil)) .to eq TwilioBase::Fake::IncomingPhoneNumber.phone_numbers.last end end end end