backup_worker_spec.rb 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe BackupWorker do
  4. let(:worker) { described_class.new }
  5. let(:service) { instance_double(BackupService, call: true) }
  6. describe '#perform' do
  7. before do
  8. allow(BackupService).to receive(:new).and_return(service)
  9. end
  10. let(:backup) { Fabricate(:backup) }
  11. let!(:other_backup) { Fabricate(:backup, user: backup.user) }
  12. it 'sends the backup to the service and removes other backups', :inline_jobs do
  13. emails = capture_emails { worker.perform(backup.id) }
  14. expect(service).to have_received(:call).with(backup)
  15. expect { other_backup.reload }.to raise_error(ActiveRecord::RecordNotFound)
  16. expect(emails.size)
  17. .to eq(1)
  18. expect(emails.first)
  19. .to have_attributes(
  20. to: contain_exactly(backup.user.email),
  21. subject: I18n.t('user_mailer.backup_ready.subject')
  22. )
  23. end
  24. context 'when sidekiq retries are exhausted' do
  25. it 'destroys the backup' do
  26. described_class.within_sidekiq_retries_exhausted_block({ 'args' => [backup.id] }) do
  27. worker.perform(backup.id)
  28. end
  29. expect { backup.reload }.to raise_error(ActiveRecord::RecordNotFound)
  30. end
  31. end
  32. end
  33. end