Add next_allocation_of spec helper
What does this MR do?
"Avoid using expect_any_instance_of
or allow_any_instance_of
" is part of our general testing guidelines. Instead, expect_next_instance_of
or allow_next_instance_of
can be used. But sometimes those do not work when records are loaded through ActiveRecord because it uses allocate
to initialize the classes but next_instance_of
stubs :new
of the given class. Since :new
is never called, it doesn't stub the method on the right instance. I ran into this problem in !33180 (comment 354146027)
This MR adds a expect_next_allocation_of
and allow_next_allocation_of
method to the NextInstanceOf
helper that can be used when models are allocated.
I changed a few specs to show examples of how to use the new helper methods
closes #220440 (closed)
Example
This works with next_instance_of
let(:project) { create(:project) }
specify do
expect_next_instance_of(Project) do |project_instance|
expect(project_instance).to receive(:add_import_job)
end
project.add_import_job
end
This works with next_allocation_of
let!(:project) { create(:project) }
specify do
expect_next_allocation_of(Project) do |project_instance|
expect(project_instance).to receive(:add_import_job)
end
Project.find(project.id).add_import_job
end
Does this MR meet the acceptance criteria?
Conformity
- [-] Changelog entry
-
Documentation (if required) -
Code review guidelines -
Merge request performance guidelines -
Style guides - [-] Database guides
- [-] Separation of EE specific content
Availability and Testing
-
Review and add/update tests for this feature/bug. Consider all test levels. See the Test Planning Process. - [-] Tested in all supported browsers
- [-] Informed Infrastructure department of a default or new setting change, if applicable per definition of done
Security
If this MR contains changes to processing or storing of credentials or tokens, authorization and authentication methods and other items described in the security review guidelines:
- [-] Label as security and @ mention
@gitlab-com/gl-security/appsec
- [-] The MR includes necessary changes to maintain consistency between UI, API, email, or other methods
- [-] Security reports checked/validated by a reviewer from the AppSec team