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