vote_spec.rb
2.73 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
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
describe Vote do
it {should belong_to :voter}
it {should belong_to :question}
it {should belong_to :prompt}
it {should belong_to :choice}
it {should belong_to :loser_choice}
it {should belong_to :appearance}
before(:each) do
@question = Factory.create(:aoi_question)
@prompt = @question.prompts.first
end
it "should create a new instance with factory girl" do
Factory.create(:vote)
end
it "should update counter cache on question" do
@question.votes.size.should == 0
@question.votes_count.should == 0
Factory.create(:vote, :question => @question)
@question.reload
@question.votes.size.should == 1
@question.votes_count.should == 1
end
it "should update counter cache on prompt" do
@prompt.votes.size.should == 0
@prompt.votes_count.should == 0
Factory.create(:vote, :question => @question, :prompt => @prompt)
@prompt.reload
@prompt.votes.size.should == 1
@prompt.votes_count.should == 1
end
it "should update counter cache on choice" do
@prompt.left_choice.votes.size.should == 0
@prompt.left_choice.votes_count.should == 0
Factory.create(:vote, :question => @question, :prompt => @prompt,
:choice => @prompt.left_choice)
@prompt.left_choice.reload
@prompt.left_choice.votes.size.should == 1
@prompt.left_choice.votes_count.should == 1
end
it "should update counter cache on loser_choice" do
@prompt.left_choice.votes.size.should == 0
@prompt.right_choice.losses.should == 0
@prompt.left_choice.votes_count.should == 0
Factory.create(:vote, :question => @question, :prompt => @prompt,
:choice => @prompt.left_choice,
:loser_choice => @prompt.right_choice)
@prompt.right_choice.reload
@prompt.right_choice.votes.size.should == 0
@prompt.right_choice.votes_count.should == 0
@prompt.right_choice.loss_count.should == 1
@prompt.right_choice.losses.should == 1
end
it "should update score of winner choice after create" do
@prompt.left_choice.score.should == 50
Factory.create(:vote, :question => @question, :prompt => @prompt,
:choice => @prompt.left_choice)
@prompt.left_choice.reload
@prompt.left_choice.score.should be_close 67, 1
end
it "should update score of loser choice after create" do
@prompt.left_choice.score.should == 50
Factory.create(:vote, :question => @question, :prompt => @prompt,
:choice => @prompt.left_choice,
:loser_choice => @prompt.right_choice)
@prompt.right_choice.reload
@prompt.right_choice.score.should be_close 33, 1
end
end