choice_spec.rb
5.02 KB
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
describe Choice do
it {should belong_to :question}
it {should belong_to :creator}
it {should have_many :flags}
it {should have_many :votes}
it {should have_many :prompts_on_the_left}
it {should have_many :prompts_on_the_right}
it {should validate_presence_of :question}
it {should validate_presence_of :creator}
before(:each) do
@aoi_clone = Factory.create(:email_confirmed_user)
@visitor= Factory.create(:visitor, :site => @aoi_clone)
@question = Factory.create(:aoi_question, :name => "Which do you like better?",
:site => @aoi_clone,
:creator => @visitor)
@valid_attributes = {
:creator => @visitor,
:question => @question,
:data => 'hi there'
}
@unreasonable_value = 9999
@protected_attributes = {}
[ :prompts_count,
:wins,
:losses,
:score,
:prompts_on_the_right_count,
:prompts_on_the_left_count
].each{|key| @protected_attributes[key] = @unreasonable_value}
end
it "should create a new instance given valid attributes" do
Choice.create!(@valid_attributes)
end
it "should not manually set protected attributes when created" do
choice1 = Choice.create!(@valid_attributes.merge(@protected_attributes))
@protected_attributes.each_key do |key|
choice1[key].should_not == @unreasonable_value
end
end
it "should not allow mass assignment of protected attributes" do
choice1 = Choice.create!(@valid_attributes)
choice1.update_attributes(@protected_attributes)
@protected_attributes.each_key do |key|
choice1[key].should_not == @unreasonable_value
end
end
it "should deactivate a choice" do
choice1 = Choice.create!(@valid_attributes.merge(:data => '1234'))
choice1.deactivate!
choice1.should_not be_active
end
it "should update a question's counter cache on creation" do
# not an allour ideas question
question = Factory.create(:question, :site => @aoi_clone, :creator => @visitor)
question.choices_count.should == 0
question.choices.size.should == 0
Choice.create!(@valid_attributes.merge(:question => question))
question.reload
question.choices_count.should == 1
question.choices.size.should == 1
end
it "should update a question's counter cache on activation" do
prev_inactive = @question.inactive_choices_count
choice1 = Choice.create!(@valid_attributes.merge(:data => '1234'))
choice1.deactivate!
@question.reload
@question.inactive_choices_count.should == prev_inactive + 1
choice1.activate!
@question.reload
@question.inactive_choices_count.should == prev_inactive
choice1.should be_active
end
it "should update a question's counter cache on deactivation" do
prev_inactive = @question.inactive_choices_count
choice1 = Choice.create!(@valid_attributes.merge(:data => '1234'))
choice1.deactivate!
@question.reload
@question.inactive_choices_count.should == prev_inactive + 1
end
it "should have a default score of 50" do
choice1 = Choice.create!(@valid_attributes)
choice1.score.should == 50
end
it "correctly compute a score based on wins and losses" do
choice1 = Choice.create!(@valid_attributes)
choice1.wins = 30
choice1.losses = 70
choice1.compute_score.should be_close(30,1)
end
it "compute score and save" do
choice1 = Choice.create!(@valid_attributes)
choice1.score.should == 50
choice1.wins = 30
choice1.losses = 70
choice1.compute_score!
choice1.score.should be_close(30, 1)
end
it "determines whether a choice is admin created" do
admin_choice = @question.choices.first
admin_choice.user_created.should be_false
end
it "determines whether a choice is user created" do
new_visitor = Factory.create(:visitor, :site => @aoi_clone)
user_choice = Factory.create(:choice, :question => @question, :creator => new_visitor)
user_choice.user_created.should be_true
end
describe "voting updates things" do
before do
@prompt = @question.choose_prompt
@winning_choice = @prompt.left_choice
@losing_choice = @prompt.right_choice
vote = Vote.create!(:choice_id => @winning_choice.id,
:loser_choice_id => @losing_choice.id,
:question_id => @question.id,
:voter_id => @visitor.id,
:prompt_id => @prompt.id )
end
it "should update score on a win" do
@winning_choice.reload
@winning_choice.score.should be_close(67, 1)
end
it "should update score on a loss" do
@losing_choice.reload
@losing_choice.score.should be_close(33,1)
end
it "should update win count on a win" do
@winning_choice.reload
@winning_choice.wins.should == 1
@winning_choice.losses.should == 0
end
it "should update loss count on a loss" do
@losing_choice.reload
@losing_choice.wins.should == 0
@losing_choice.losses.should == 1
end
end
end