# 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 describe '.incoming_phone_number_list' do it 'returns active numbers' do phone_numbers = [] 3.times do phone_numbers << TwilioBase::Fake::IncomingPhoneNumber::PhoneNumber.new( Faker::PhoneNumber.phone_number ) end TwilioBase::Fake::IncomingPhoneNumber.phone_numbers = phone_numbers expect(described_class.incoming_phone_number_list) .to match_array(phone_numbers) end end end