1
0

poll_spec.rb 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe Poll do
  4. describe 'Scopes' do
  5. let(:status) { Fabricate(:status) }
  6. let(:attached_poll) { Fabricate(:poll, status: status) }
  7. let(:not_attached_poll) do
  8. Fabricate(:poll).tap do |poll|
  9. poll.status = nil
  10. poll.save(validate: false)
  11. end
  12. end
  13. describe '.attached' do
  14. it 'finds the correct records' do
  15. results = described_class.attached
  16. expect(results).to eq([attached_poll])
  17. end
  18. end
  19. describe '.unattached' do
  20. it 'finds the correct records' do
  21. results = described_class.unattached
  22. expect(results).to eq([not_attached_poll])
  23. end
  24. end
  25. end
  26. describe '#reset_votes!' do
  27. let(:poll) { Fabricate :poll, cached_tallies: [2, 3], votes_count: 5, voters_count: 5 }
  28. let!(:vote) { Fabricate :poll_vote, poll: }
  29. it 'resets vote data and deletes votes' do
  30. expect { poll.reset_votes! }
  31. .to change(poll, :cached_tallies).to([0, 0])
  32. .and change(poll, :votes_count).to(0)
  33. .and(change(poll, :voters_count).to(0))
  34. expect { vote.reload }
  35. .to raise_error(ActiveRecord::RecordNotFound)
  36. end
  37. end
  38. describe 'Validations' do
  39. subject { Fabricate.build(:poll) }
  40. it { is_expected.to validate_presence_of(:expires_at) }
  41. end
  42. end