2015-01-18 05:15:21 -08:00
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
RSpec.describe Question, :type => :model do
|
|
|
|
before :each do
|
|
|
|
@question = Question.new(
|
|
|
|
content: 'Is this a question?',
|
2020-04-19 08:27:42 -07:00
|
|
|
user: FactoryBot.create(:user)
|
2015-01-18 05:15:21 -08:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
subject { @question }
|
|
|
|
|
|
|
|
it { should respond_to(:content) }
|
|
|
|
|
|
|
|
it '#content returns a string' do
|
|
|
|
expect(@question.content).to match 'Is this a question?'
|
|
|
|
end
|
|
|
|
|
2022-01-10 13:01:47 -08:00
|
|
|
it 'does not save questions longer than 512 characters' do
|
|
|
|
@question.content = 'X' * 513
|
2015-01-18 05:15:21 -08:00
|
|
|
expect{@question.save!}.to raise_error(ActiveRecord::RecordInvalid)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'has many answers' do
|
2020-04-19 08:27:42 -07:00
|
|
|
5.times { |i| Answer.create(content: "This is an answer. #{i}", user: FactoryBot.create(:user), question: @question) }
|
2015-01-18 05:15:21 -08:00
|
|
|
expect(@question.answer_count).to match 5
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'also deletes the answers when deleted' do
|
2020-04-19 08:27:42 -07:00
|
|
|
5.times { |i| Answer.create(content: "This is an answer. #{i}", user: FactoryBot.create(:user), question: @question) }
|
2015-01-18 05:15:21 -08:00
|
|
|
first_answer_id = @question.answers.first.id
|
|
|
|
@question.destroy
|
|
|
|
expect{Answer.find(first_answer_id)}.to raise_error(ActiveRecord::RecordNotFound)
|
|
|
|
end
|
|
|
|
end
|