forked from brian-lc/surveyor
/
question_spec.rb
108 lines (91 loc) · 3.34 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
describe Question, "when creating a new question" do
before(:each) do
@ss = mock_model(SurveySection)
@question = Question.new(:text => "What is your favorite color?", :survey_section => @ss, :is_mandatory => true, :display_order => 1)
end
it "should be invalid without text" do
@question.text = nil
@question.should have(1).error_on(:text)
end
it "should have a parent survey section" do
# this causes issues with building and saving
# @question.survey_section = nil
# @question.should have(1).error_on(:survey_section_id)
end
it "should be mandatory by default" do
@question.mandatory?.should be_true
end
it "should convert pick attribute to string" do
@question.pick.should == "none"
@question.pick = :one
@question.pick.should == "one"
@question.pick = nil
@question.pick.should == nil
end
it "should split the text" do
@question.split_text.should == "What is your favorite color?"
@question.split_text(:pre).should == "What is your favorite color?"
@question.split_text(:post).should == ""
@question.text = "before|after|extra"
@question.split_text.should == "before|after|extra"
@question.split_text(:pre).should == "before"
@question.split_text(:post).should == "after|extra"
end
it "should have an api_id" do
@question.api_id.length.should == 36
end
end
describe Question, "that has answers" do
before(:each) do
@question = Factory(:question, :text => "What is your favorite color?")
Factory(:answer, :question => @question, :display_order => 3, :text => "blue")
Factory(:answer, :question => @question, :display_order => 1, :text => "red")
Factory(:answer, :question => @question, :display_order => 2, :text => "green")
end
it "should have answers" do
@question.answers.should have(3).answers
end
it "should retrieve those answers in display_order" do
@question.answers.map(&:display_order).should == [1,2,3]
end
it "should delete answers when it is deleted" do
answer_ids = @question.answers.map(&:id)
@question.destroy
answer_ids.each{|id| Answer.find_by_id(id).should be_nil}
end
end
describe Question, "when interacting with an instance" do
before(:each) do
@question = Factory(:question)
end
it "should return 'default' for nil display type" do
@question.display_type = nil
@question.renderer.should == :default
end
it "should let you know if it is part of a group" do
@question.question_group = Factory(:question_group)
@question.solo?.should be_false
@question.part_of_group?.should be_true
@question.question_group = nil
@question.solo?.should be_true
@question.part_of_group?.should be_false
end
end
describe Question, "with dependencies" do
before(:each) do
@rs = mock_model(ResponseSet)
@question = Factory(:question)
end
it "should check its dependency" do
@dependency = mock_model(Dependency)
@dependency.stub!(:is_met?).with(@rs).and_return(true)
@question.stub!(:dependency).and_return(@dependency)
@question.triggered?(@rs).should == true
end
it "should delete dependency when it is deleted" do
dep_id = Factory(:dependency, :question => @question).id
@question.destroy
Dependency.find_by_id(dep_id).should be_nil
end
end