Test `ShareWorker`'s handling of unhandled exceptions

This commit is contained in:
Karina Kwiatek 2021-12-27 23:02:32 +01:00
parent 0b926a43e4
commit d73e269d85
1 changed files with 16 additions and 8 deletions

View File

@ -5,21 +5,29 @@ require 'rails_helper'
describe ShareWorker do describe ShareWorker do
let(:user) { FactoryBot.create(:user) } let(:user) { FactoryBot.create(:user) }
let(:answer) { FactoryBot.create(:answer, user: user) } let(:answer) { FactoryBot.create(:answer, user: user) }
let!(:service) { Services::Twitter.create!(type: 'Services::Twitter',
user: user) }
describe "#perform" do describe "#perform" do
subject { ShareWorker.new.perform(user.id, answer.id, 'twitter') } subject {
Sidekiq::Testing.fake! do
before do ShareWorker.perform_async(user.id, answer.id, 'twitter')
Service.create!(type: 'Services::Twitter', end
user: user) }
end
context 'when answer doesn\'t exist' do context 'when answer doesn\'t exist' do
it 'prevents the job from retrying and logs a message' do it 'prevents the job from retrying and logs a message' do
answer.destroy! answer.destroy!
Sidekiq.logger.should_receive(:info) Sidekiq.logger.should_receive(:info)
subject expect { subject }.to change(ShareWorker.jobs, :size).by(1)
expect(ShareWorker.jobs.size).to eq(0) expect { ShareWorker.drain }.to change(ShareWorker.jobs, :size).by(-1)
end
end
context 'when answer exists' do
it 'retries on unhandled exceptions' do
expect { subject }.to change(ShareWorker.jobs, :size).by(1)
expect { ShareWorker.drain }.to raise_error(Twitter::Error::Forbidden)
end end
end end
end end