Welcome to mirror list, hosted at ThFree Co, Russian Federation.

next_instance_of.rb « helpers « support « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a8e9ab2bafea3aca833a4a5c5ec73057a6060883 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# frozen_string_literal: true

module NextInstanceOf
  def expect_next_instance_of(klass, *new_args, &blk)
    stub_new(expect(klass), nil, *new_args, &blk)
  end

  def expect_next_instances_of(klass, number, *new_args, &blk)
    stub_new(expect(klass), number, *new_args, &blk)
  end

  def allow_next_instance_of(klass, *new_args, &blk)
    stub_new(allow(klass), nil, *new_args, &blk)
  end

  def allow_next_instances_of(klass, number, *new_args, &blk)
    stub_new(allow(klass), number, *new_args, &blk)
  end

  private

  def stub_new(target, number, *new_args, &blk)
    receive_new = receive(:new)
    receive_new.exactly(number).times if number
    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(&blk)
    end
  end
end