forked from expertiza/reimplementation-back-end
-
Notifications
You must be signed in to change notification settings - Fork 1
/
question_spec.rb
63 lines (53 loc) · 3.02 KB
/
question_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
require 'rails_helper'
RSpec.describe Question, type: :model do
# Creating dummy objects for the test with the help of let statement
let(:role) {Role.create(name: 'Instructor', parent_id: nil, id: 2, default_page_id: nil)}
let(:instructor) { Instructor.create(id: 1234, name: 'testinstructor', email: 'test@test.com', fullname: 'Test Instructor', password: '123456', role: role) }
let(:questionnaire) { Questionnaire.new id:1, name: 'abc', private: 0, min_question_score: 0, max_question_score: 10, instructor_id: instructor.id }
describe "validations" do
# Test validates that question has valid attributes
it "is valid with valid attributes" do
question = Question.new(seq: 1, txt: "Sample question", question_type: "multiple_choice", break_before: true, questionnaire: questionnaire)
expect(question).to be_valid
end
# Test ensures that a question is not valid without seq field
it "is not valid without a seq" do
question = Question.new(txt: "Sample question", question_type: "multiple_choice", break_before: true, questionnaire: questionnaire)
expect(question).to_not be_valid
end
# Test ensures that seq field is numeric
it "is not valid with a non-numeric seq" do
question = Question.new(seq: "one", txt: "Sample question", question_type: "multiple_choice", break_before: true, questionnaire: questionnaire)
expect(question).to_not be_valid
end
# Test ensures that a question is not valid without txt field
it "is not valid without a txt" do
question = Question.new(seq: 1, question_type: "multiple_choice", break_before: true, questionnaire: questionnaire)
expect(question).to_not be_valid
end
# Test ensures that a question is not valid without question_type field
it "is not valid without a question_type" do
question = Question.new(seq: 1, txt: "Sample question", break_before: true, questionnaire: questionnaire)
expect(question).to_not be_valid
end
# Test ensures that a question is not valid without break_before field
it "is not valid without a break_before value" do
question = Question.new(seq: 1, txt: "Sample question", question_type: "multiple_choice", questionnaire: questionnaire)
expect(question).to_not be_valid
end
# Test ensures that a question does not exist without a questionnaire
it "is not valid without a questionnaire" do
question = Question.new(seq: 1, txt: "Sample question", question_type: "multiple_choice", break_before: true)
expect(question).to_not be_valid
end
end
describe "#delete" do
# Test ensures that a question object is deleted properly taking all its association into consideration
it "destroys the question object" do
instructor.save!
questionnaire.save!
question = Question.create(seq: 1, txt: "Sample question", question_type: "multiple_choice", break_before: true, questionnaire: questionnaire)
expect { question.delete }.to change { Question.count }.by(-1)
end
end
end