acts_as_follower_test.rb
6.57 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
require File.dirname(__FILE__) + '/test_helper'
class ActsAsFollowerTest < ActiveSupport::TestCase
context "instance methods" do
setup do
@sam = FactoryGirl.create(:sam)
end
should "be defined" do
assert @sam.respond_to?(:following?)
assert @sam.respond_to?(:follow_count)
assert @sam.respond_to?(:follow)
assert @sam.respond_to?(:stop_following)
assert @sam.respond_to?(:follows_by_type)
assert @sam.respond_to?(:all_follows)
end
end
context "acts_as_follower" do
setup do
@sam = FactoryGirl.create(:sam)
@jon = FactoryGirl.create(:jon)
@oasis = FactoryGirl.create(:oasis)
@sam.follow(@jon)
@sam.follow(@oasis)
end
context "following" do
should "return following_status" do
assert_equal true, @sam.following?(@jon)
assert_equal false, @jon.following?(@sam)
end
should "return follow_count" do
assert_equal 2, @sam.follow_count
assert_equal 0, @jon.follow_count
end
end
context "follow a friend" do
setup do
@jon.follow(@sam)
end
should_change("Follow count", by: 1) { Follow.count }
should_change("@jon.follow_count", by: 1) { @jon.follow_count }
should "set the follower" do
assert_equal @jon, Follow.last.follower
end
should "set the followable" do
assert_equal @sam, Follow.last.followable
end
end
context "follow yourself" do
setup do
@jon.follow(@jon)
end
should_not_change("Follow count") { Follow.count }
should_not_change("@jon.follow_count") { @jon.follow_count }
should "not set the follower" do
assert_not_equal @jon, Follow.last.follower
end
should "not set the followable" do
assert_not_equal @jon, Follow.last.followable
end
end
context "stop_following" do
setup do
@sam.stop_following(@jon)
end
should_change("Follow count", by: -1) { Follow.count }
should_change("@sam.follow_count", by: -1) { @sam.follow_count }
end
context "follows" do
setup do
@band_follow = Follow.where("follower_id = ? and follower_type = 'User' and followable_id = ? and followable_type = 'Band'", @sam.id, @oasis.id).first
@user_follow = Follow.where("follower_id = ? and follower_type = 'User' and followable_id = ? and followable_type = 'User'", @sam.id, @jon.id).first
end
context "follows_by_type" do
should "only return requested follows" do
assert_equal [@band_follow], @sam.follows_by_type('Band')
assert_equal [@user_follow], @sam.follows_by_type('User')
end
should "accept AR options" do
@metallica = FactoryGirl.create(:metallica)
@sam.follow(@metallica)
assert_equal 1, @sam.follows_by_type('Band', limit: 1).count
end
end
context "following_by_type_count" do
should "return the count of the requested type" do
@metallica = FactoryGirl.create(:metallica)
@sam.follow(@metallica)
assert_equal 2, @sam.following_by_type_count('Band')
assert_equal 1, @sam.following_by_type_count('User')
assert_equal 0, @jon.following_by_type_count('Band')
@jon.block(@sam)
assert_equal 0, @sam.following_by_type_count('User')
end
end
context "all_follows" do
should "return all follows" do
assert_equal 2, @sam.all_follows.size
assert @sam.all_follows.include?(@band_follow)
assert @sam.all_follows.include?(@user_follow)
assert_equal [], @jon.all_follows
end
should "accept AR options" do
assert_equal 1, @sam.all_follows(limit: 1).count
end
end
end
context "all_following" do
should "return the actual follow records" do
assert_equal 2, @sam.all_following.size
assert @sam.all_following.include?(@oasis)
assert @sam.all_following.include?(@jon)
assert_equal [], @jon.all_following
end
should "accept AR limit option" do
assert_equal 1, @sam.all_following(limit: 1).count
end
should "accept AR where option" do
assert_equal 1, @sam.all_following(where: { id: @oasis.id }).count
end
end
context "following_by_type" do
should "return only requested records" do
assert_equal [@oasis], @sam.following_by_type('Band')
assert_equal [@jon], @sam.following_by_type('User')
end
should "accept AR options" do
@metallica = FactoryGirl.create(:metallica)
@sam.follow(@metallica)
assert_equal 1, @sam.following_by_type('Band', limit: 1).to_a.size
end
end
context "method_missing" do
should "call following_by_type" do
assert_equal [@oasis], @sam.following_bands
assert_equal [@jon], @sam.following_users
end
should "call following_by_type_count" do
@metallica = FactoryGirl.create(:metallica)
@sam.follow(@metallica)
assert_equal 2, @sam.following_bands_count
assert_equal 1, @sam.following_users_count
assert_equal 0, @jon.following_bands_count
@jon.block(@sam)
assert_equal 0, @sam.following_users_count
end
should "raise on no method" do
assert_raises (NoMethodError){ @sam.foobar }
end
end
context "respond_to?" do
should "advertise that it responds to following methods" do
assert @sam.respond_to?(:following_users)
assert @sam.respond_to?(:following_users_count)
end
should "return false when called with a nonexistent method" do
assert (not @sam.respond_to?(:foobar))
end
end
context "destroying follower" do
setup do
@jon.destroy
end
should_change("Follow.count", by: -1) { Follow.count }
should_change("@sam.follow_count", by: -1) { @sam.follow_count }
end
context "blocked by followable" do
setup do
@jon.block(@sam)
end
should "return following_status" do
assert_equal false, @sam.following?(@jon)
end
should "return follow_count" do
assert_equal 1, @sam.follow_count
end
should "not return record of the blocked follows" do
assert_equal 1, @sam.all_follows.size
assert !@sam.all_follows.include?(@user_follow)
assert !@sam.all_following.include?(@jon)
assert_equal [], @sam.following_by_type('User')
assert_equal [], @sam.follows_by_type('User')
assert_equal [], @sam.following_users
end
end
end
end