Disallow stubbing in factories
<!--IssueSummary start--> <details> <summary> Everyone can contribute. [Help move this issue forward](https://handbook.gitlab.com/handbook/marketing/developer-relations/contributor-success/community-contributors-workflows/#contributor-links) while earning points, leveling up and collecting rewards. </summary> - [Close this issue](https://contributors.gitlab.com/manage-issue?action=close&projectId=278964&issueIid=340487) </details> <!--IssueSummary end--> ## 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 https://gitlab.com/gitlab-org/gitlab/-/issues/247111 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 - [x] Document that `allow` is discouraged in factories - https://gitlab.com/gitlab-org/gitlab/-/merge_requests/69965 - [ ] Suggest alternatives to stubbing in factories - [ ] Create a :cop: RuboCop rule to ban the use of `allow` and `expect` in factories - [ ] Remove remaining offenses
issue
Advertisement
Advertisement