Commit 9fcc3630 authored by Arturo Herrero's avatar Arturo Herrero

RSpec helpers: Extract duplicated code

expect_next_instance_of and allow_next_instance_of use the same code
expect the target (expect or allow).
parent 35735620
...@@ -90,8 +90,7 @@ RSpec.configure do |config| ...@@ -90,8 +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 AllowNextInstanceOf config.include NextInstanceOf
config.include ExpectNextInstanceOf
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 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
# 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