From 35735620a551bf272b3142b5bd646efbc149eab9 Mon Sep 17 00:00:00 2001 From: Arturo Herrero Date: Fri, 4 Oct 2019 17:43:57 +0100 Subject: [PATCH 1/3] Create allow_next_instance_of spec helper --- doc/development/gotchas.md | 2 +- spec/spec_helper.rb | 1 + spec/support/helpers/allow_next_instance_of.rb | 15 +++++++++++++++ 3 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 spec/support/helpers/allow_next_instance_of.rb diff --git a/doc/development/gotchas.md b/doc/development/gotchas.md index 32efef2a9b3c54..5dd201a956662b 100644 --- a/doc/development/gotchas.md +++ b/doc/development/gotchas.md @@ -106,7 +106,7 @@ 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: diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index bf87c078ac6fbe..451eedac9c163b 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -90,6 +90,7 @@ config.include StubFeatureFlags config.include StubGitlabCalls config.include StubGitlabData + config.include AllowNextInstanceOf config.include ExpectNextInstanceOf config.include TestEnv config.include Devise::Test::ControllerHelpers, type: :controller diff --git a/spec/support/helpers/allow_next_instance_of.rb b/spec/support/helpers/allow_next_instance_of.rb new file mode 100644 index 00000000000000..7160139f1d3941 --- /dev/null +++ b/spec/support/helpers/allow_next_instance_of.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +module AllowNextInstanceOf + def allow_next_instance_of(klass, *new_args) + receive_new = receive(:new) + receive_new.with(*new_args) if new_args.any? + + allow(klass).to receive_new + .and_wrap_original do |method, *original_args| + method.call(*original_args).tap do |instance| + yield(instance) + end + end + end +end -- GitLab From 9fcc3630697ade0c8ebe3bd829d1fe0cd9af148c Mon Sep 17 00:00:00 2001 From: Arturo Herrero Date: Tue, 8 Oct 2019 19:43:16 +0100 Subject: [PATCH 2/3] RSpec helpers: Extract duplicated code expect_next_instance_of and allow_next_instance_of use the same code expect the target (expect or allow). --- spec/spec_helper.rb | 3 +- .../support/helpers/allow_next_instance_of.rb | 15 ---------- .../helpers/expect_next_instance_of.rb | 15 ---------- spec/support/helpers/next_instance_of.rb | 28 +++++++++++++++++++ 4 files changed, 29 insertions(+), 32 deletions(-) delete mode 100644 spec/support/helpers/allow_next_instance_of.rb delete mode 100644 spec/support/helpers/expect_next_instance_of.rb create mode 100644 spec/support/helpers/next_instance_of.rb diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 451eedac9c163b..948e5e6250b205 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -90,8 +90,7 @@ config.include StubFeatureFlags config.include StubGitlabCalls config.include StubGitlabData - config.include AllowNextInstanceOf - 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/allow_next_instance_of.rb b/spec/support/helpers/allow_next_instance_of.rb deleted file mode 100644 index 7160139f1d3941..00000000000000 --- a/spec/support/helpers/allow_next_instance_of.rb +++ /dev/null @@ -1,15 +0,0 @@ -# frozen_string_literal: true - -module AllowNextInstanceOf - def allow_next_instance_of(klass, *new_args) - receive_new = receive(:new) - receive_new.with(*new_args) if new_args.any? - - allow(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/expect_next_instance_of.rb b/spec/support/helpers/expect_next_instance_of.rb deleted file mode 100644 index 749d2cb2a56976..00000000000000 --- 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 00000000000000..83c788c3d38542 --- /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 -- GitLab From cf8d8a842c097a21e2cf5e784137d9fd07267ddb Mon Sep 17 00:00:00 2001 From: Arturo Herrero Date: Thu, 10 Oct 2019 09:07:09 +0100 Subject: [PATCH 3/3] Docs Gotchas: Add allow_next_instance_of example --- doc/development/gotchas.md | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/doc/development/gotchas.md b/doc/development/gotchas.md index 5dd201a956662b..5557a113d0524e 100644 --- a/doc/development/gotchas.md +++ b/doc/development/gotchas.md @@ -113,6 +113,8 @@ 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: -- GitLab