Skip to content

Commit 5840e69

Browse files
committed
Optimise performance with let_it_be and before_all
1 parent a267b6d commit 5840e69

File tree

6 files changed

+69
-58
lines changed

6 files changed

+69
-58
lines changed

app/services/exporter/service.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ class Service
55
attr_reader :user
66

77
EXPORTERS = [
8-
Articles,
9-
Comments,
8+
Exporter::Articles,
9+
Exporter::Comments,
1010
].freeze
1111

1212
def initialize(user)

spec/controllers/internal_users_controller_spec.rb

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
require "rails_helper"
22

33
RSpec.describe "internal/users", type: :request do
4-
let(:user) { create(:user) }
5-
let(:user2) { create(:user) }
6-
let(:user3) { create(:user) }
7-
let(:super_admin) { create(:user, :super_admin) }
8-
let(:article) { create(:article, user: user) }
9-
let(:article2) { create(:article, user: user2) }
4+
let_it_be(:user) { create(:user) }
5+
let_it_be(:user2) { create(:user) }
6+
let_it_be(:user3) { create(:user) }
7+
let_it_be(:super_admin) { create(:user, :super_admin) }
8+
let_it_be(:article) { create(:article, user: user) }
9+
let_it_be(:article2) { create(:article, user: user2) }
1010

1111
before do
1212
sign_in super_admin
13-
user
14-
user2
15-
Delayed::Worker.new(quiet: true).work_off
13+
end
14+
15+
before_all do
1616
dependents_for_offending_user_article
1717
offender_activity_on_other_content
1818
end
@@ -28,7 +28,6 @@ def dependents_for_offending_user_article
2828
create(:reaction, reactable: comment2, reactable_type: "Comment", user: user2)
2929
# create user3 reaction to offending article
3030
create(:reaction, reactable: article, reactable_type: "Article", user: user3, category: "like")
31-
Delayed::Worker.new(quiet: true).work_off
3231
end
3332

3433
def offender_activity_on_other_content
@@ -38,7 +37,6 @@ def offender_activity_on_other_content
3837
comment = create(:comment, commentable_type: "Article", commentable: article2, user: user)
3938
# user3 reacts to offender comment
4039
create(:reaction, reactable: comment, reactable_type: "Comment", user: user3)
41-
Delayed::Worker.new(quiet: true).work_off
4240
end
4341

4442
context "when deleting user" do
@@ -62,7 +60,6 @@ def create_mutual_follows
6260
create_mutual_follows
6361
create_mention
6462
create(:badge_achievement, rewarder_id: 1, rewarding_context_message: "yay", user_id: user.id)
65-
Delayed::Worker.new(quiet: true).work_off
6663
end
6764

6865
it "raises a 'record not found' error after deletion" do
@@ -90,7 +87,6 @@ def create_mutual_follows
9087
context "when banishing user" do
9188
def banish_user
9289
post "/internal/users/#{user.id}/banish"
93-
Delayed::Worker.new(quiet: true).work_off
9490
user.reload
9591
end
9692

spec/models/article_spec.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ def build_and_validate_article(*args)
88
article
99
end
1010

11-
let(:user) { create(:user) }
11+
let_it_be(:user) { create(:user) }
1212
let(:article) { create(:article, user_id: user.id) }
1313

1414
it { is_expected.to validate_uniqueness_of(:canonical_url).allow_blank }
@@ -40,7 +40,7 @@ def build_and_validate_article(*args)
4040
end
4141

4242
it "reject future dates" do
43-
expect(build(:article, with_date: true, date: "01/01/2020").valid?).to be(false)
43+
expect(build(:article, with_date: true, date: DateTime.now + 1.day).valid?).to be(false)
4444
end
4545

4646
it "has proper username" do
@@ -107,7 +107,7 @@ def build_and_validate_article(*args)
107107
end
108108

109109
describe "#slug" do
110-
let(:title) { "hey This' is$ a SLUG" }
110+
let_it_be(:title) { "hey This' is$ a SLUG" }
111111
let(:article0) { build(:article, title: title, published: false) }
112112
let(:article1) { build(:article, title: title, published: false) }
113113

@@ -142,8 +142,8 @@ def build_and_validate_article(*args)
142142

143143
context "when provided with body_markdown" do
144144
let(:test_article) { build(:article, title: title) }
145-
let(:title) { "Talk About It, Justify It" }
146-
let(:slug) { "talk-about-it-justify-it" }
145+
let_it_be(:title) { "Talk About It, Justify It" }
146+
let_it_be(:slug) { "talk-about-it-justify-it" }
147147

148148
before { test_article.validate }
149149

@@ -175,7 +175,7 @@ def build_and_validate_article(*args)
175175
end
176176

177177
describe "#canonical_url" do
178-
let(:article_with_canon_url) { build(:article, with_canonical_url: true) }
178+
let_it_be(:article_with_canon_url) { build(:article, with_canonical_url: true) }
179179

180180
before do
181181
article_with_canon_url.validate
@@ -431,7 +431,7 @@ def build_and_validate_article(*args)
431431
end
432432

433433
context "when unpublished" do
434-
let(:article) { create(:article, published: false) }
434+
let_it_be(:article) { create(:article, published: false) }
435435

436436
it "does not update the hotness score" do
437437
article.save

spec/models/comment_spec.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
require "rails_helper"
22

33
RSpec.describe Comment, type: :model do
4-
let(:user) { create(:user, created_at: 3.weeks.ago) }
5-
let(:user2) { create(:user) }
6-
let(:article) { create(:article, user_id: user.id, published: true) }
7-
let(:article_with_video) { create(:article, :video, user_id: user.id, published: true) }
8-
let(:comment) { create(:comment, user_id: user2.id, commentable_id: article.id) }
9-
let(:video_comment) { create(:comment, user_id: user2.id, commentable_id: article_with_video.id) }
10-
let(:comment_2) { create(:comment, user_id: user2.id, commentable_id: article.id) }
11-
let(:child_comment) do
4+
let_it_be(:user) { create(:user, created_at: 3.weeks.ago) }
5+
let_it_be(:user2) { create(:user) }
6+
let_it_be(:article) { create(:article, user_id: user.id, published: true) }
7+
let_it_be(:article_with_video) { create(:article, :video, user_id: user.id, published: true) }
8+
let_it_be(:comment) { create(:comment, user_id: user2.id, commentable_id: article.id) }
9+
let_it_be(:video_comment) { create(:comment, user_id: user2.id, commentable_id: article_with_video.id) }
10+
let_it_be(:comment_2) { create(:comment, user_id: user2.id, commentable_id: article.id) }
11+
let_it_be(:child_comment) do
1212
build(:comment, user_id: user.id, commentable_id: article.id, parent_id: comment.id)
1313
end
1414

spec/rails_helper.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
require "pundit/rspec"
1313
require "webmock/rspec"
1414
require "test_prof/recipes/rspec/before_all"
15+
require "test_prof/recipes/rspec/let_it_be"
1516
require "test_prof/recipes/rspec/sample"
1617

1718
# Requires supporting ruby files with custom matchers and macros, etc, in

spec/requests/notifications_spec.rb

Lines changed: 42 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
require "rails_helper"
22

33
RSpec.describe "NotificationsIndex", type: :request do
4-
let(:dev_account) { create(:user) }
5-
let(:user) { create(:user) }
4+
let_it_be(:dev_account) { create(:user) }
5+
let_it_be(:user) { create(:user) }
66

77
before do
88
allow(User).to receive(:dev_account).and_return(dev_account)
@@ -71,9 +71,9 @@ def mock_follow_notifications(amount)
7171
end
7272

7373
context "when a user has new reaction notifications" do
74-
let(:article1) { create(:article, user_id: user.id) }
75-
let(:article2) { create(:article, user_id: user.id) }
76-
let(:special_characters_article) { create(:article, user_id: user.id, title: "What's Become of Waring") }
74+
let_it_be(:article1) { create(:article, user_id: user.id) }
75+
let_it_be(:article2) { create(:article, user_id: user.id) }
76+
let_it_be(:special_characters_article) { create(:article, user_id: user.id, title: "What's Become of Waring") }
7777

7878
before { sign_in user }
7979

@@ -141,29 +141,32 @@ def mock_heart_reaction_notifications(amount, categories, reactable = article1)
141141
end
142142

143143
context "when a user has a new comment notification" do
144-
let(:user2) { create(:user) }
145-
let(:article) { create(:article, user_id: user.id) }
146-
let(:comment) { create(:comment, user_id: user2.id, commentable_id: article.id, commentable_type: "Article") }
144+
let_it_be(:user2) { create(:user) }
145+
let_it_be(:article) { create(:article, user_id: user.id) }
146+
let_it_be(:comment) { create(:comment, user_id: user2.id, commentable_id: article.id, commentable_type: "Article") }
147147

148148
before do
149149
sign_in user
150150
Notification.send_new_comment_notifications_without_delay(comment)
151-
get "/notifications"
152151
end
153152

154153
it "renders the correct message" do
154+
get "/notifications"
155155
expect(response.body).to include "commented on"
156156
end
157157

158158
it "does not render the moderation message" do
159+
get "/notifications"
159160
expect(response.body).not_to include "As a trusted member"
160161
end
161162

162163
it "renders the article's path" do
164+
get "/notifications"
163165
expect(response.body).to include article.path
164166
end
165167

166168
it "renders the comment's processed HTML" do
169+
get "/notifications"
167170
expect(response.body).to include comment.processed_html
168171
end
169172

@@ -174,22 +177,26 @@ def mock_heart_reaction_notifications(amount, categories, reactable = article1)
174177
end
175178

176179
it "does not render the reaction as reacted if it was not reacted on" do
180+
get "/notifications"
177181
expect(response.body).not_to include "reaction-button reacted"
178182
end
179183
end
180184

181185
context "when a user has a new moderation notification" do
182-
let(:user2) { create(:user) }
183-
let(:article) { create(:article, user_id: user.id) }
184-
let(:comment) { create(:comment, user_id: user2.id, commentable_id: article.id, commentable_type: "Article") }
186+
let_it_be(:user2) { create(:user) }
187+
let_it_be(:article) { create(:article, user_id: user.id) }
188+
let_it_be(:comment) { create(:comment, user_id: user2.id, commentable_id: article.id, commentable_type: "Article") }
185189

186190
before do
187-
user.add_role :trusted
188191
sign_in user
189192
Notification.send_moderation_notification_without_delay(comment)
190193
get "/notifications"
191194
end
192195

196+
before_all do
197+
user.add_role :trusted
198+
end
199+
193200
it "renders the proper message" do
194201
expect(response.body).to include "As a trusted member"
195202
end
@@ -204,23 +211,25 @@ def mock_heart_reaction_notifications(amount, categories, reactable = article1)
204211
end
205212

206213
context "when a user has a new welcome notification" do
214+
let_it_be(:broadcast) { create(:broadcast, :onboarding) }
215+
207216
before do
208217
sign_in user
218+
Notification.send_welcome_notification_without_delay(user.id)
219+
get "/notifications"
209220
end
210221

211222
it "renders the welcome notification" do
212-
broadcast = create(:broadcast, :onboarding)
213-
Notification.send_welcome_notification_without_delay(user.id)
214-
get "/notifications"
215223
expect(response.body).to include broadcast.processed_html
216224
end
217225
end
218226

219227
context "when a user has a new badge notification" do
228+
let_it_be(:badge) { create(:badge) }
229+
let_it_be(:badge_achievement) { create(:badge_achievement, user: user, badge: badge) }
230+
220231
before do
221232
sign_in user
222-
badge = create(:badge)
223-
badge_achievement = create(:badge_achievement, user: user, badge: badge)
224233
Notification.send_new_badge_notification_without_delay(badge_achievement)
225234
get "/notifications"
226235
end
@@ -243,9 +252,9 @@ def mock_heart_reaction_notifications(amount, categories, reactable = article1)
243252
end
244253

245254
context "when a user has a new mention notification" do
246-
let(:user2) { create(:user) }
247-
let(:article) { create(:article, user_id: user.id) }
248-
let(:comment) do
255+
let_it_be(:user2) { create(:user) }
256+
let_it_be(:article) { create(:article, user_id: user.id) }
257+
let_it_be(:comment) do
249258
create(
250259
:comment,
251260
user_id: user2.id,
@@ -256,13 +265,15 @@ def mock_heart_reaction_notifications(amount, categories, reactable = article1)
256265
end
257266

258267
before do
259-
comment
260-
Mention.create_all_without_delay(comment)
261-
Notification.send_mention_notification_without_delay(Mention.first)
262268
sign_in user
269+
Notification.send_mention_notification_without_delay(Mention.first)
263270
get "/notifications"
264271
end
265272

273+
before_all do
274+
Mention.create_all_without_delay(comment)
275+
end
276+
266277
it "renders the proper message" do
267278
expect(response.body).to include "mentioned you in a comment"
268279
end
@@ -273,16 +284,19 @@ def mock_heart_reaction_notifications(amount, categories, reactable = article1)
273284
end
274285

275286
context "when a user has a new article notification" do
276-
let(:user2) { create(:user) }
277-
let(:article) { create(:article, user_id: user.id) }
287+
let_it_be(:user2) { create(:user) }
288+
let_it_be(:article) { create(:article, user_id: user.id) }
278289

279290
before do
280-
user2.follow(user)
281-
Notification.send_to_followers_without_delay(article, "Published")
282291
sign_in user2
292+
Notification.send_to_followers_without_delay(article, "Published")
283293
get "/notifications"
284294
end
285295

296+
before_all do
297+
user2.follow(user)
298+
end
299+
286300
it "renders the proper message" do
287301
expect(response.body).to include "made a new post:"
288302
end

0 commit comments

Comments
 (0)