From d74c2c583a0bee57c042bf957ee864e17b53472a Mon Sep 17 00:00:00 2001 From: Matt Jankowski Date: Tue, 15 Oct 2024 08:48:10 -0400 Subject: [PATCH] Extend spec coverage for `Poll` model (#32500) --- spec/models/poll_spec.rb | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/spec/models/poll_spec.rb b/spec/models/poll_spec.rb index 736f3615d..66f521ab3 100644 --- a/spec/models/poll_spec.rb +++ b/spec/models/poll_spec.rb @@ -3,7 +3,7 @@ require 'rails_helper' RSpec.describe Poll do - describe 'scopes' do + describe 'Scopes' do let(:status) { Fabricate(:status) } let(:attached_poll) { Fabricate(:poll, status: status) } let(:not_attached_poll) do @@ -13,7 +13,7 @@ RSpec.describe Poll do end end - describe 'attached' do + describe '.attached' do it 'finds the correct records' do results = described_class.attached @@ -21,7 +21,7 @@ RSpec.describe Poll do end end - describe 'unattached' do + describe '.unattached' do it 'finds the correct records' do results = described_class.unattached @@ -30,11 +30,23 @@ RSpec.describe Poll do end end - describe 'validations' do - context 'when not valid' do - subject { Fabricate.build(:poll) } + describe '#reset_votes!' do + let(:poll) { Fabricate :poll, cached_tallies: [2, 3], votes_count: 5, voters_count: 5 } + let!(:vote) { Fabricate :poll_vote, poll: } - it { is_expected.to validate_presence_of(:expires_at) } + it 'resets vote data and deletes votes' do + expect { poll.reset_votes! } + .to change(poll, :cached_tallies).to([0, 0]) + .and change(poll, :votes_count).to(0) + .and(change(poll, :voters_count).to(0)) + expect { vote.reload } + .to raise_error(ActiveRecord::RecordNotFound) end end + + describe 'Validations' do + subject { Fabricate.build(:poll) } + + it { is_expected.to validate_presence_of(:expires_at) } + end end