acts_as_followable_test.rb
7.04 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
require File.dirname(__FILE__) + '/test_helper'
class ActsAsFollowableTest < ActiveSupport::TestCase
context "instance methods" do
setup do
@sam = FactoryGirl.create(:sam)
end
should "be defined" do
assert @sam.respond_to?(:followers_count)
assert @sam.respond_to?(:followers)
assert @sam.respond_to?(:followed_by?)
end
end
context "acts_as_followable" do
setup do
@sam = FactoryGirl.create(:sam)
@jon = FactoryGirl.create(:jon)
@oasis = FactoryGirl.create(:oasis)
@metallica = FactoryGirl.create(:metallica)
@sam.follow(@jon)
end
context "followers_count" do
should "return the number of followers" do
assert_equal 0, @sam.followers_count
assert_equal 1, @jon.followers_count
end
should "return the proper number of multiple followers" do
@bob = FactoryGirl.create(:bob)
@sam.follow(@bob)
assert_equal 0, @sam.followers_count
assert_equal 1, @jon.followers_count
assert_equal 1, @bob.followers_count
end
end
context "followers" do
should "return users" do
assert_equal [], @sam.followers
assert_equal [@sam], @jon.followers
end
should "return users (multiple followers)" do
@bob = FactoryGirl.create(:bob)
@sam.follow(@bob)
assert_equal [], @sam.followers
assert_equal [@sam], @jon.followers
assert_equal [@sam], @bob.followers
end
should "return users (multiple followers, complex)" do
@bob = FactoryGirl.create(:bob)
@sam.follow(@bob)
@jon.follow(@bob)
assert_equal [], @sam.followers
assert_equal [@sam], @jon.followers
assert_equal [@sam, @jon], @bob.followers
end
should "accept AR options" do
@bob = FactoryGirl.create(:bob)
@bob.follow(@jon)
assert_equal 1, @jon.followers(:limit => 1).count
end
end
context "followed_by" do
should "return_follower_status" do
assert_equal true, @jon.followed_by?(@sam)
assert_equal false, @sam.followed_by?(@jon)
end
end
context "destroying a followable" do
setup do
@jon.destroy
end
should_change("follow count", :by => -1) { Follow.count }
should_change("@sam.all_following.size", :by => -1) { @sam.all_following.size }
end
context "get follow record" do
setup do
@bob = FactoryGirl.create(:bob)
@follow = @bob.follow(@sam)
end
should "return follow record" do
assert_equal @follow, @sam.get_follow_for(@bob)
end
should "return nil" do
assert_nil @sam.get_follow_for(@jon)
end
end
context "blocks" do
setup do
@bob = FactoryGirl.create(:bob)
@jon.block(@sam)
@jon.block(@bob)
end
should "accept AR options" do
assert_equal 1, @jon.blocks(:limit => 1).count
end
end
context "blocking a follower" do
context "in my following list" do
setup do
@jon.block(@sam)
end
should "remove him from followers" do
assert_equal 0, @jon.followers_count
end
should "add him to the blocked followers" do
assert_equal 1, @jon.blocked_followers_count
end
should "not be able to follow again" do
@jon.follow(@sam)
assert_equal 0, @jon.followers_count
end
should "not be present when listing followers" do
assert_equal [], @jon.followers
end
should "be in the list of blocks" do
assert_equal [@sam], @jon.blocks
end
end
context "not in my following list" do
setup do
@sam.block(@jon)
end
should "add him to the blocked followers" do
assert_equal 1, @sam.blocked_followers_count
end
should "not be able to follow again" do
@sam.follow(@jon)
assert_equal 0, @sam.followers_count
end
should "not be present when listing followers" do
assert_equal [], @sam.followers
end
should "be in the list of blocks" do
assert_equal [@jon], @sam.blocks
end
end
end
context "unblocking a blocked follow" do
setup do
@jon.block(@sam)
@jon.unblock(@sam)
end
should "not include the unblocked user in the list of followers" do
assert_equal [], @jon.followers
end
should "remove him from the blocked followers" do
assert_equal 0, @jon.blocked_followers_count
assert_equal [], @jon.blocks
end
end
context "unblock a non-existent follow" do
setup do
@sam.stop_following(@jon)
@jon.unblock(@sam)
end
should "not be in the list of followers" do
assert_equal [], @jon.followers
end
should "not be in the blocked followers count" do
assert_equal 0, @jon.blocked_followers_count
end
should "not be in the blocks list" do
assert_equal [], @jon.blocks
end
end
context "followers_by_type" do
setup do
@sam.follow(@oasis)
@jon.follow(@oasis)
end
should "return the followers for given type" do
assert_equal [@sam], @jon.followers_by_type('User')
assert_equal [@sam, @jon], @oasis.followers_by_type('User')
end
should "not return block followers in the followers for a given type" do
@oasis.block(@jon)
assert_equal [@sam], @oasis.followers_by_type('User')
end
should "return the count for followers_by_type_count for a given type" do
assert_equal 1, @jon.followers_by_type_count('User')
assert_equal 2, @oasis.followers_by_type_count('User')
end
should "not count blocked follows in the count" do
@oasis.block(@sam)
assert_equal 1, @oasis.followers_by_type_count('User')
end
end
context "method_missing" do
setup do
@sam.follow(@oasis)
@jon.follow(@oasis)
end
should "return the followers for given type" do
assert_equal [@sam], @jon.user_followers
assert_equal [@sam, @jon], @oasis.user_followers
end
should "not return block followers in the followers for a given type" do
@oasis.block(@jon)
assert_equal [@sam], @oasis.user_followers
end
should "return the count for followers_by_type_count for a given type" do
assert_equal 1, @jon.count_user_followers
assert_equal 2, @oasis.count_user_followers
end
should "not count blocked follows in the count" do
@oasis.block(@sam)
assert_equal 1, @oasis.count_user_followers
end
end
context "respond_to?" do
should "advertise that it responds to following methods" do
assert @oasis.respond_to?(:user_followers)
assert @oasis.respond_to?(:user_followers_count)
end
should "return false when called with a nonexistent method" do
assert (not @oasis.respond_to?(:foobar))
end
end
end
end