Update rails version, fix some broken specs and remove some warnings
[shapado:piglops-shapado.git] / spec / models / answer_spec.rb
1 require 'spec_helper'
2
3 describe Answer do
4   before(:each) do
5     @current_user = User.make
6     Thread.current[:current_user] = @current_user
7     @answer = Answer.make(:votes => {}, :question => Question.make)
8   end
9
10   describe "module/plugin inclusions (optional)" do
11   end
12
13   describe "validations" do
14     it "the answer of a user in a question should be unique" do
15       answer = Answer.make_unsaved(:question => @answer.question,
16                                    :created_at => @answer.created_at+1.day,
17                                    :user => @answer.user,
18                                    :group_id => @answer.group_id,
19                                    :votes => {})
20
21       answer.valid?.should be_false
22       answer.errors[:limitation].should_not be_nil
23     end
24
25     it "elapsed time between two answers by the same user should be greater than 20 secs" do
26       answer = Answer.make_unsaved(:question => @answer.question,
27                                    :created_at => @answer.created_at+1,
28                                    :user => @answer.user,
29                                    :group => @answer.group,
30                                    :votes => {})
31       answer.valid?.should be_false
32     end
33   end
34
35   describe "association" do
36   end
37
38   describe "callbacks" do
39     describe "Answer#unsolve_question" do
40       it "should set the answer's question as unsolved when the question is deleted" do
41         question = @answer.question
42         question.answer = @answer
43         question.accepted = true
44         question.save
45         question.reload
46
47         question.accepted.should be_true
48         question.answer.should_not be_nil
49
50         @answer.destroy
51
52         question.reload
53         question.accepted.should be_false
54         question.answer.should be_nil
55       end
56     end
57   end
58
59   describe "named scopes" do
60   end
61
62   describe "class methods" do
63     describe "Answer#minimal" do
64       it "should return a answer context without some keys" do
65         Answer.should_receive(:without).with(:_keywords,
66                                              :flags,
67                                              :votes,
68                                              :versions)
69         Answer.minimal
70       end
71     end
72
73     describe "Answer#ban" do
74       it "should ban the answer" do
75         @answer.banned.should be_false
76         Answer.ban([@answer.id])
77         @answer.reload
78         @answer.banned.should be_true
79       end
80     end
81
82     describe "Answer#unban" do
83       it "should unban the answer" do
84         @answer.ban
85         @answer.reload
86         Answer.unban([@answer.id])
87         @answer.reload
88         @answer.banned.should be_false
89       end
90     end
91   end
92
93   describe "instance methods" do
94     describe "Answer#ban" do
95       it "should ban the answer" do
96         @answer.banned.should be_false
97         @answer.ban
98         @answer.reload
99         @answer.banned.should be_true
100       end
101     end
102
103     describe "Answer#unban" do
104       it "should unban the answer" do
105         @answer.ban
106         @answer.unban
107         @answer.reload
108         @answer.banned.should be_false
109       end
110     end
111
112     describe "Answer#can_be_deleted_by?" do
113       before(:each) do
114         @user = User.make
115         @answer.question.closed = true
116       end
117
118       after(:each) do
119         @user.destroy
120       end
121
122       describe "should return true when the user " do
123         it "is the answer's creator and he can delete his own comments" do
124           @answer.user.should_receive(:can_delete_own_comments_on?).with(anything).and_return(true)
125           @answer.can_be_deleted_by?(@answer.user).should == true
126         end
127
128         it "is the question's creator and he can delete comments on his own questions" do
129           user = @answer.question.user
130           user.should_receive(:can_delete_comments_on_own_questions_on?).
131                                 with(anything).and_return(true)
132           @answer.can_be_deleted_by?(user).should == true
133         end
134       end
135     end
136
137     describe "Answer#on_add_vote" do
138     end
139
140     describe "Answer#on_remove_vote" do
141     end
142
143     describe "Answer#flagged!" do
144       it "should increment the answer's flags counter" do
145         @answer.flags_count.should == 0
146         @answer.flagged!
147         @answer.reload
148         @answer.flags_count.should == 1
149       end
150     end
151
152     describe "Answer#to_html" do
153     end
154
155     describe "Answer#disable_limits?" do
156     end
157
158     describe "Answer#add_favorite!" do
159     end
160
161     describe "Answer#remove_favorite!" do
162     end
163
164     describe "Answer#favorite_for?" do
165     end
166   end
167 end