Commit 3fecaf59 authored by Thong Kuah's avatar Thong Kuah

Freeze objects when when calling create_default

This prevents state leakage across specs, especially when used with
factory_default: :keep
parent 13a4a9d9
......@@ -176,9 +176,9 @@ it as `namespace: namespace`. In order to make it work along with `let_it_be`, `
must be explicitly specified. That keeps the default factory for every example in a suite instead of
recreating it for each example.
Objects created inside a `factory_default: :keep`, and using
`create_default` inside a `let_it_be` should be frozen to prevent accidental reliance
between test examples.
To prevent accidental reliance between test examples, objects created
with `create_default` are
[frozen](https://gitlab.com/gitlab-org/gitlab/-/blob/master/spec/support/factory_default.rb).
Maybe we don't need to create 208 different projects - we
can create one and reuse it. In addition, we can see that only about 1/3 of the
......
......@@ -65,10 +65,16 @@ RSpec.describe SlackService do
end
context 'wiki_page notification' do
let_it_be(:wiki_page) { create(:wiki_page, wiki: project.wiki, message: 'user created page: Awesome wiki_page') }
let(:wiki_page) { create(:wiki_page, wiki: project.wiki, message: 'user created page: Awesome wiki_page') }
let(:data) { Gitlab::DataBuilder::WikiPage.build(wiki_page, user, 'create') }
before do
# Skip this method that is not relevant to this test to prevent having
# to update project which is frozen
allow(project.wiki).to receive(:after_wiki_activity)
end
it_behaves_like 'increases the usage data counter', 'i_ecosystem_slack_service_wiki_page_notification'
end
......
# frozen_string_literal: true
module Gitlab
module FreezeFactoryDefault
def set_factory_default(name, obj, preserve_traits: nil)
obj.freeze unless obj.frozen?
super
end
end
end
TestProf::FactoryDefault::DefaultSyntax.prepend Gitlab::FreezeFactoryDefault
RSpec.configure do |config|
config.after do |ex|
TestProf::FactoryDefault.reset unless ex.metadata[:factory_default] == :keep
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment