create_statistics_worker_spec.rb 946 Bytes
Newer Older
1 2 3 4
# frozen_string_literal: true

require 'spec_helper'

5
RSpec.describe Users::CreateStatisticsWorker do
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
  describe '#perform' do
    subject { described_class.new.perform }

    before do
      allow(ActiveRecord::Base.connection).to receive(:transaction_open?).and_return(false)
    end

    context 'when successful' do
      it 'create an users statistics entry' do
        expect { subject }.to change { UsersStatistics.count }.from(0).to(1)
      end
    end

    context 'when unsuccessful' do
      it 'logs an error' do
        users_statistics = build(:users_statistics)
        users_statistics.errors.add(:base, 'This is an error')
        exception = ActiveRecord::RecordInvalid.new(users_statistics)

        allow(UsersStatistics).to receive(:create_current_stats!).and_raise(exception)

        expect(Gitlab::ErrorTracking).to receive(:track_exception).with(exception).and_call_original

        subject
      end
    end
  end
end