Sha256: dae94f1f669a179740f89752fcdbeecd4a95ff5817b9f4593bb72827cc34540c

Contents?: true

Size: 1.83 KB

Versions: 1

Compression:

Stored size: 1.83 KB

Contents

require 'spec_helper'

describe Spree::Question do

  let!(:question_category) { create(:question_category) }
  let(:question) { create(:question, question_category_id: question_category.id) }

  subject { question }

  context 'factory' do
    it 'is valid' do
      expect(build(:question)).to be_valid
    end
  end

  context 'instance attributes' do
    it 'create a new instance given valid attributes' do
      described_class.create!(question: 'Question 1',
                              answer: 'Answer 1',
                              question_category: create(:question_category))
    end
  end

  context 'relation' do
    it { should belong_to(:question_category) }

    it 'belong to a category' do
      expect(subject.question_category).not_to be_nil
    end
  end

  context 'validation' do
    it { should validate_presence_of(:question_category_id) }
    it { should validate_presence_of(:question) }
    it { should validate_presence_of(:answer) }

    it 'require a category' do
      invalid_question = build(:question, question_category: nil)
      expect(invalid_question).to have(1).error_on(:question_category_id)
    end

    it 'require a question' do
      invalid_question = build(:question, question: nil)
      expect(invalid_question).to have(1).error_on(:question)
    end

    it 'require a answer' do
      invalid_question = build(:question, answer: nil)
      expect(invalid_question).to have(1).error_on(:answer)
    end
  end

  context 'mass assignment' do
    %w(question answer question_category_id question_category).each do |column|
      it { should allow_mass_assignment_of(column.to_sym) }
    end
  end

  context 'acts as list' do

    before do
      2.times { create(:question) }
    end

    it 'can have its position changed' do
      subject.move_to_bottom
      expect(subject.position).to eq(3)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
spree-faq-2.0.0 spec/models/question_spec.rb