Disallow stubbing in factories
Problem
Stubbing in factories doesn't play well with let_it_be
and lead to errors like:
Failure/Error: allow(merge_request).to receive(:fetch_ref!)
The use of doubles or partial doubles from rspec-mocks outside of the per-test lifecycle is not supported.
See #247111 (closed) and https://gitlab.com/gitlab-org/gitlab/-/blob/00a771a3cfc095c6904b6b13f59f3435762175ae/spec/factories/merge_requests.rb#L319-324.
let_it_be
helps us to improve our test efficiency. See https://gitlab.com/gitlab-org/plan/-/issues/145.
Current use
Likely, stubbing in factories is only used rarely as of today:
spec/factories/deployments.rb
spec/factories/commits.rb
spec/factories/projects.rb
spec/factories/container_repositories.rb
spec/factories/merge_requests.rb
spec/factories/clusters/applications/helm.rb
spec/factories/ci/builds.rb
Proposed solution
-
Document that allow
is discouraged in factories - !69965 (merged) -
Suggest alternatives to stubbing in factories -
Create a 👮 RuboCop rule to ban the use ofallow
andexpect
in factories -
Remove remaining offenses