diff --git a/doc/development/gotchas.md b/doc/development/gotchas.md index 32efef2a9b3c54732ff5d9abf2155145ca5c67b6..5557a113d0524e4e6348d5fb3260d1539785d8b8 100644 --- a/doc/development/gotchas.md +++ b/doc/development/gotchas.md @@ -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. ``` -### Alternative: `expect_next_instance_of` +### Alternative: `expect_next_instance_of` or `allow_next_instance_of` Instead of writing: ```ruby # Don't do this: expect_any_instance_of(Project).to receive(:add_import_job) + +allow_any_instance_of(Project).to receive(:add_import_job) ``` We could write: @@ -122,10 +124,14 @@ We could write: expect_next_instance_of(Project) do |project| expect(project).to receive(:add_import_job) 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 -arguments, we could also pass it to `expect_next_instance_of` like: +If we also want to initialized the instance with some particular arguments, we +could also pass it like: ```ruby # Do this: diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index bf87c078ac6fbe5ed8d37362a86fb2ad8cc38b4c..948e5e6250b20596618331d9319528520f6d33a0 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -90,7 +90,7 @@ config.include StubFeatureFlags config.include StubGitlabCalls config.include StubGitlabData - config.include ExpectNextInstanceOf + config.include NextInstanceOf config.include TestEnv config.include Devise::Test::ControllerHelpers, type: :controller config.include Devise::Test::IntegrationHelpers, type: :feature diff --git a/spec/support/helpers/expect_next_instance_of.rb b/spec/support/helpers/expect_next_instance_of.rb deleted file mode 100644 index 749d2cb2a5697604975b704f35c38fd8ba7176c7..0000000000000000000000000000000000000000 --- a/spec/support/helpers/expect_next_instance_of.rb +++ /dev/null @@ -1,15 +0,0 @@ -# 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 diff --git a/spec/support/helpers/next_instance_of.rb b/spec/support/helpers/next_instance_of.rb new file mode 100644 index 0000000000000000000000000000000000000000..83c788c3d3854242fb351aec305efa1db0f57b46 --- /dev/null +++ b/spec/support/helpers/next_instance_of.rb @@ -0,0 +1,28 @@ +# 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