docbrown/spec/models/poll_skip_spec.rb
Jeremy Friesen 2a26fa3538
Refactoring poll/vote/skip logic (#15441)
* Refactoring poll/vote/skip logic

Favor asking the poll if the user has already voted instead of relying
on the poll vote or skip to know about the implementation
details (e.g. should a poll vote know that there's a related model call
poll skipped?  likely not)

This refactor also tidies up the specs, for which there's quite a bit of
chatter.

Found via the `flay` gem.

* Adding method documentation

Also need to bump gitpod's build as it halted for the prior commit.
2021-11-29 10:52:32 -05:00

27 lines
926 B
Ruby

require "rails_helper"
RSpec.describe PollSkip, type: :model do
let(:article) { create(:article, featured: true) }
let(:user) { create(:user) }
let(:poll) { create(:poll, article: article) }
context "when user has already voted (or skipped) on the poll" do
it "is invalid" do
vote = build(:poll_skip, user: user, poll: poll)
allow(vote.poll).to receive(:vote_previously_recorded_for?).with(user_id: user.id).and_return(true)
expect(vote).not_to be_valid
expect(vote.errors[:base]).to include("cannot vote more than once in one poll")
end
end
context "when user has not voted nor skipped the poll" do
it "is valid" do
vote = build(:poll_skip, user: user, poll: poll)
allow(vote.poll).to receive(:vote_previously_recorded_for?).with(user_id: user.id).and_return(false)
expect(vote).to be_valid
expect(vote.errors[:base]).to be_empty
end
end
end