adds comprehensive validation specs
This commit is contained in:
@@ -8,7 +8,8 @@ class Poll::Answer < ActiveRecord::Base
|
||||
validates :question, presence: true
|
||||
validates :author, presence: true
|
||||
validates :answer, presence: true
|
||||
validates :answer, inclusion: {in: ->(a) { a.question.valid_answers }}
|
||||
validates :answer, inclusion: { in: ->(a) { a.question.valid_answers }},
|
||||
unless: ->(a) { a.question.blank? }
|
||||
|
||||
scope :by_author, ->(author_id) { where(author_id: author_id) }
|
||||
scope :by_question, ->(question_id) { where(question_id: question_id) }
|
||||
|
||||
@@ -3,13 +3,35 @@ require 'rails_helper'
|
||||
describe Poll::Answer do
|
||||
|
||||
describe "validations" do
|
||||
it "validates that the answers are included in the Poll::Question's list" do
|
||||
q = create(:poll_question, valid_answers: 'One, Two, Three')
|
||||
expect(build(:poll_answer, question: q, answer: 'One')).to be_valid
|
||||
expect(build(:poll_answer, question: q, answer: 'Two')).to be_valid
|
||||
expect(build(:poll_answer, question: q, answer: 'Three')).to be_valid
|
||||
|
||||
expect(build(:poll_answer, question: q, answer: 'Four')).to_not be_valid
|
||||
let(:answer) { build(:poll_answer) }
|
||||
|
||||
it "should be valid" do
|
||||
expect(answer).to be_valid
|
||||
end
|
||||
|
||||
it "should not be valid wihout a question" do
|
||||
answer.question = nil
|
||||
expect(answer).to_not be_valid
|
||||
end
|
||||
|
||||
it "should not be valid without an author" do
|
||||
answer.author = nil
|
||||
expect(answer).to_not be_valid
|
||||
end
|
||||
|
||||
it "should not be valid without an answer" do
|
||||
answer.answer = nil
|
||||
expect(answer).to_not be_valid
|
||||
end
|
||||
|
||||
it "should be valid for answers included in the Poll::Question's list" do
|
||||
question = create(:poll_question, valid_answers: 'One, Two, Three')
|
||||
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')).to_not be_valid
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user