Commit 6d395185 authored by Douglas Barbosa Alexandre's avatar Douglas Barbosa Alexandre

Merge branch '25470-allow-any-instance-of' into 'master'

Create allow_next_instance_of spec helper

Closes #25470

See merge request gitlab-org/gitlab!18139
parents 6a675725 cf8d8a84
...@@ -106,13 +106,15 @@ end ...@@ -106,13 +106,15 @@ end
Using `any_instance` to stub a method (elasticsearch_indexing) that has been defined on a prepended module (EE::ApplicationSetting) is not supported. Using `any_instance` to stub a method (elasticsearch_indexing) that has been defined on a prepended module (EE::ApplicationSetting) is not supported.
``` ```
### Alternative: `expect_next_instance_of` ### Alternative: `expect_next_instance_of` or `allow_next_instance_of`
Instead of writing: Instead of writing:
```ruby ```ruby
# Don't do this: # Don't do this:
expect_any_instance_of(Project).to receive(:add_import_job) expect_any_instance_of(Project).to receive(:add_import_job)
allow_any_instance_of(Project).to receive(:add_import_job)
``` ```
We could write: We could write:
...@@ -122,10 +124,14 @@ We could write: ...@@ -122,10 +124,14 @@ We could write:
expect_next_instance_of(Project) do |project| expect_next_instance_of(Project) do |project|
expect(project).to receive(:add_import_job) expect(project).to receive(:add_import_job)
end end
allow_next_instance_of(Project) do |project|
allow(project).to receive(:add_import_job)
end
``` ```
If we also want to expect the instance was initialized with some particular If we also want to initialized the instance with some particular arguments, we
arguments, we could also pass it to `expect_next_instance_of` like: could also pass it like:
```ruby ```ruby
# Do this: # Do this:
......
...@@ -90,7 +90,7 @@ RSpec.configure do |config| ...@@ -90,7 +90,7 @@ RSpec.configure do |config|
config.include StubFeatureFlags config.include StubFeatureFlags
config.include StubGitlabCalls config.include StubGitlabCalls
config.include StubGitlabData config.include StubGitlabData
config.include ExpectNextInstanceOf config.include NextInstanceOf
config.include TestEnv config.include TestEnv
config.include Devise::Test::ControllerHelpers, type: :controller config.include Devise::Test::ControllerHelpers, type: :controller
config.include Devise::Test::IntegrationHelpers, type: :feature config.include Devise::Test::IntegrationHelpers, type: :feature
......
# frozen_string_literal: true
module ExpectNextInstanceOf
def expect_next_instance_of(klass, *new_args)
receive_new = receive(:new)
receive_new.with(*new_args) if new_args.any?
expect(klass).to receive_new
.and_wrap_original do |method, *original_args|
method.call(*original_args).tap do |instance|
yield(instance)
end
end
end
end
# frozen_string_literal: true
module NextInstanceOf
def expect_next_instance_of(klass, *new_args)
stub_new(expect(klass), *new_args) do |expectation|
yield(expectation)
end
end
def allow_next_instance_of(klass, *new_args)
stub_new(allow(klass), *new_args) do |allowance|
yield(allowance)
end
end
private
def stub_new(target, *new_args)
receive_new = receive(:new)
receive_new.with(*new_args) if new_args.any?
target.to receive_new.and_wrap_original do |method, *original_args|
method.call(*original_args).tap do |instance|
yield(instance)
end
end
end
end
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