Files
grecia/spec/models/poll/answer_spec.rb
Javi Martín db97f9d08c Add and apply rubocop rules for empty lines
We were very inconsistent regarding these rules.

Personally I prefer no empty lines around blocks, clases, etc... as
recommended by the Ruby style guide [1], and they're the default values
in rubocop, so those are the settings I'm applying.

The exception is the `private` access modifier, since we were leaving
empty lines around it most of the time. That's the default rubocop rule
as well. Personally I don't have a strong preference about this one.


[1] https://rubystyle.guide/#empty-lines-around-bodies
2019-10-24 17:11:47 +02:00

75 lines
2.4 KiB
Ruby

require "rails_helper"
describe Poll::Answer do
describe "validations" do
let(:answer) { build(:poll_answer) }
it "is valid" do
expect(answer).to be_valid
end
it "is not valid wihout a question" do
answer.question = nil
expect(answer).not_to be_valid
end
it "is not valid without an author" do
answer.author = nil
expect(answer).not_to be_valid
end
it "is not valid without an answer" do
answer.answer = nil
expect(answer).not_to be_valid
end
it "is valid for answers included in the Poll::Question's question_answers list" do
question = create(:poll_question)
create(:poll_question_answer, title: "One", question: question)
create(:poll_question_answer, title: "Two", question: question)
create(:poll_question_answer, title: "Three", question: question)
expect(build(:poll_answer, question: question, answer: "One")).to be_valid
expect(build(:poll_answer, question: question, answer: "Two")).to be_valid
expect(build(:poll_answer, question: question, answer: "Three")).to be_valid
expect(build(:poll_answer, question: question, answer: "Four")).not_to be_valid
end
end
describe "#record_voter_participation" do
let(:author) { create(:user, :level_two) }
let(:poll) { create(:poll) }
let(:question) { create(:poll_question, :yes_no, poll: poll) }
it "creates a poll_voter with user and poll data" do
answer = create(:poll_answer, question: question, author: author, answer: "Yes")
expect(answer.poll.voters).to be_blank
answer.record_voter_participation("token")
expect(poll.reload.voters.size).to eq(1)
voter = poll.voters.first
expect(voter.document_number).to eq(answer.author.document_number)
expect(voter.poll_id).to eq(answer.poll.id)
expect(voter.officer_id).to eq(nil)
end
it "updates a poll_voter with user and poll data" do
answer = create(:poll_answer, question: question, author: author, answer: "Yes")
answer.record_voter_participation("token")
expect(poll.reload.voters.size).to eq(1)
answer = create(:poll_answer, question: question, author: author, answer: "No")
answer.record_voter_participation("token")
expect(poll.reload.voters.size).to eq(1)
voter = poll.voters.first
expect(voter.document_number).to eq(answer.author.document_number)
expect(voter.poll_id).to eq(answer.poll.id)
end
end
end