Sha256: 5b3c4c295755d6e1e6cd70e67feb91a61f3d8afdbab4ff7d13ef368c861b185c

Contents?: true

Size: 1.65 KB

Versions: 2

Compression:

Stored size: 1.65 KB

Contents

require 'rails_helper'

module Quizzes
  RSpec.describe Question, type: :model do

    it { should validate_presence_of(:text) }
    it { should validate_uniqueness_of(:text) }

    it "limits the number of allowed answers" do
      question = FactoryGirl.create(:question)

      question.answers.create({ text: "Answer - 1", correct: true })
      question.answers.create({ text: "Answer - 2", correct: false })
      question.answers.create({ text: "Answer - 3", correct: false })
      question.answers.create({ text: "Answer - 4", correct: false })

      expect {
        question.answers.create({ text: "Answer - 5", correct: false })
      }.to raise_error("This question has reached the maximum number of allowed answers")
    end

    it "allows only one correct answer" do
      question = FactoryGirl.create(:question)

      question.answers.create({ text: "Answer - 1", correct: false })
      question.answers.create({ text: "Answer - 2", correct: false })
      question.answers.create({ text: "Answer - 3", correct: true })

      expect {
        question.answers.create({ text: "Answer - 4", correct: true })
      }.to raise_error("A question can have only one correct answer")
    end

    it "ensures at least one correct answer" do
      question = FactoryGirl.create(:question)

      question.answers.create({ text: "Answer - 1", correct: false })
      question.answers.create({ text: "Answer - 2", correct: false })
      question.answers.create({ text: "Answer - 3", correct: false })

      expect {
        question.answers.create({ text: "Answer - 4", correct: false })
      }.to raise_error("This question needs at least one correct answer")
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
quizzes-0.1.1 spec/models/quizzes/question_spec.rb
quizzes-0.1.0 spec/models/quizzes/question_spec.rb