DEV: Fabricate instead of just building topic, post and user in tests (#18698)

Building does not persist the object in the database which is
unrealistic since we're mostly dealing with persisted objects in
production.

In theory, this will result our test suite taking longer to run since we
now have to write to the database. However, I don't expect the increase
to be significant and it is actually no different than us adding new
tests which fabricates more objects.
This commit is contained in:
Alan Guo Xiang Tan
2022-10-24 05:51:41 +08:00
committed by GitHub
parent c85e3e8083
commit c752d68737
3 changed files with 62 additions and 68 deletions
+5 -5
View File
@@ -129,11 +129,11 @@ RSpec.describe PostSerializer do
end
context "with a hidden post with add_raw enabled" do
let(:user) { Fabricate.build(:user, id: -99999) }
let(:user) { Fabricate(:user, id: -99999) }
let(:raw) { "Raw contents of the post." }
context "with a public post" do
let(:post) { Fabricate.build(:post, raw: raw, user: user) }
let(:post) { Fabricate(:post, raw: raw, user: user) }
it "includes the raw post for everyone" do
[nil, user, Fabricate(:user), Fabricate(:moderator), Fabricate(:admin)].each do |user|
@@ -143,7 +143,7 @@ RSpec.describe PostSerializer do
end
context "with a hidden post" do
let(:post) { Fabricate.build(:post, raw: raw, user: user, hidden: true, hidden_reason_id: Post.hidden_reasons[:flag_threshold_reached]) }
let(:post) { Fabricate(:post, raw: raw, user: user, hidden: true, hidden_reason_id: Post.hidden_reasons[:flag_threshold_reached]) }
it "shows the raw post only if authorized to see it" do
expect(serialized_post_for_user(nil)[:raw]).to eq(nil)
@@ -186,7 +186,7 @@ RSpec.describe PostSerializer do
end
context "with a public wiki post" do
let(:post) { Fabricate.build(:post, raw: raw, user: user, wiki: true) }
let(:post) { Fabricate(:post, raw: raw, user: user, wiki: true) }
it "can view edit history" do
[nil, user, Fabricate(:user), Fabricate(:moderator), Fabricate(:admin)].each do |user|
@@ -197,7 +197,7 @@ RSpec.describe PostSerializer do
context "with a hidden wiki post" do
let(:post) {
Fabricate.build(
Fabricate(
:post,
raw: raw,
user: user,