Skip to content

Commit a02b62a

Browse files
committed
Optimise build_stubbed
1 parent 5840e69 commit a02b62a

File tree

2 files changed

+12
-14
lines changed

2 files changed

+12
-14
lines changed

spec/models/article_spec.rb

+3-5
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ def build_and_validate_article(*args)
314314
end
315315

316316
it "removes from search index" do
317-
article = create(:article)
317+
article = build_stubbed(:article)
318318
article.remove_algolia_index
319319
end
320320

@@ -351,9 +351,7 @@ def build_and_validate_article(*args)
351351
end
352352

353353
describe "::filter_excluded_tags" do
354-
before do
355-
create(:article, tags: "hiring")
356-
end
354+
let_it_be(:article) { create(:article, tags: "hiring") }
357355

358356
it "exlude #hiring when no argument is given" do
359357
expect(described_class.filter_excluded_tags.length).to be(0)
@@ -379,7 +377,7 @@ def build_and_validate_article(*args)
379377
expect(article.path).to eq("/#{article.username}/#{article.slug}")
380378
end
381379
it "assigns cached_user_name on save" do
382-
article = create(:article, user_id: user.id)
380+
article = build_stubbed(:article, user_id: user.id)
383381
expect(article.cached_user_name).to eq(article.cached_user_name)
384382
end
385383
it "assigns cached_user_username on save" do

spec/models/notification_spec.rb

+9-9
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
RSpec.describe Notification, type: :model do
44
let(:user) { create(:user) }
55
let(:user2) { create(:user) }
6-
let(:user3) { create(:user) }
7-
let(:organization) { create(:organization) }
6+
let_it_be(:user3) { create(:user) }
7+
let_it_be(:organization) { create(:organization) }
88
let(:article) { create(:article, user_id: user.id, page_views_count: 4000, positive_reactions_count: 70) }
99
let(:follow_instance) { user.follow(user2) }
1010

@@ -86,19 +86,19 @@
8686
end
8787

8888
it "does not send a notification to the author of the article if the commenter is the author" do
89-
comment = create(:comment, user: user, commentable: article)
89+
comment = build_stubbed(:comment, user: user, commentable: article)
9090
Notification.send_new_comment_notifications_without_delay(comment)
9191
expect(user.notifications.count).to eq 0
9292
end
9393

9494
it "does not send a notification to the author of the comment" do
95-
comment = create(:comment, user: user2, commentable: article)
95+
comment = build_stubbed(:comment, user: user2, commentable: article)
9696
Notification.send_new_comment_notifications_without_delay(comment)
9797
expect(user2.notifications.count).to eq 0
9898
end
9999

100100
it "sends a notification to the organization" do
101-
org = create(:organization)
101+
org = build_stubbed(:organization)
102102
user.update(organization: org)
103103
article.update(organization: org)
104104
comment = create(:comment, user: user2, commentable: article)
@@ -110,7 +110,7 @@
110110
context "when the author of the article is not subscribed" do
111111
it "does not send a notification to the author of the article" do
112112
article.update(receive_notifications: false)
113-
comment = create(:comment, user: user2, commentable: article)
113+
comment = build_stubbed(:comment, user: user2, commentable: article)
114114
Notification.send_new_comment_notifications_without_delay(comment)
115115
expect(user.notifications.count).to eq 0
116116
end
@@ -120,7 +120,7 @@
120120
it "does not send a notification to the author of the comment" do
121121
parent_comment = create(:comment, user: user2, commentable: article)
122122
parent_comment.update(receive_notifications: false)
123-
child_comment = create(:comment, user: user, commentable: article, ancestry: parent_comment.id.to_s)
123+
child_comment = build_stubbed(:comment, user: user, commentable: article, ancestry: parent_comment.id.to_s)
124124
Notification.send_new_comment_notifications_without_delay(child_comment)
125125
expect(user2.notifications.count).to eq 0
126126
end
@@ -147,14 +147,14 @@
147147
it "does not send a notification to the author of a comment" do
148148
comment = create(:comment, user: user2, commentable: article)
149149
comment.update(receive_notifications: false)
150-
reaction = create(:reaction, reactable: comment, user: user)
150+
reaction = build_stubbed(:reaction, reactable: comment, user: user)
151151
Notification.send_reaction_notification_without_delay(reaction, reaction.reactable.user)
152152
expect(user2.notifications.count).to eq 0
153153
end
154154

155155
it "does not send a notification to the author of an article" do
156156
article.update(receive_notifications: false)
157-
reaction = create(:reaction, reactable: article, user: user2)
157+
reaction = build_stubbed(:reaction, reactable: article, user: user2)
158158
Notification.send_reaction_notification_without_delay(reaction, reaction.reactable.user)
159159
expect(user.notifications.count).to eq 0
160160
end

0 commit comments

Comments
 (0)