2019-12-21 20:55:43 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module NextInstanceOf
|
2021-02-22 17:27:13 +05:30
|
|
|
def expect_next_instance_of(klass, *new_args, &blk)
|
|
|
|
stub_new(expect(klass), nil, *new_args, &blk)
|
2019-12-21 20:55:43 +05:30
|
|
|
end
|
|
|
|
|
2021-02-22 17:27:13 +05:30
|
|
|
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)
|
2019-12-21 20:55:43 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2021-02-22 17:27:13 +05:30
|
|
|
def stub_new(target, number, *new_args, &blk)
|
2019-12-21 20:55:43 +05:30
|
|
|
receive_new = receive(:new)
|
2021-02-22 17:27:13 +05:30
|
|
|
receive_new.exactly(number).times if number
|
2019-12-21 20:55:43 +05:30
|
|
|
receive_new.with(*new_args) if new_args.any?
|
|
|
|
|
|
|
|
target.to receive_new.and_wrap_original do |method, *original_args|
|
2021-02-22 17:27:13 +05:30
|
|
|
method.call(*original_args).tap(&blk)
|
2019-12-21 20:55:43 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|