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)
|
2021-04-29 21:17:54 +05:30
|
|
|
stub_new(expect(klass), nil, false, *new_args, &blk)
|
2019-12-21 20:55:43 +05:30
|
|
|
end
|
|
|
|
|
2021-04-29 21:17:54 +05:30
|
|
|
def expect_next_instances_of(klass, number, ordered = false, *new_args, &blk)
|
|
|
|
stub_new(expect(klass), number, ordered, *new_args, &blk)
|
2021-02-22 17:27:13 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def allow_next_instance_of(klass, *new_args, &blk)
|
2021-04-29 21:17:54 +05:30
|
|
|
stub_new(allow(klass), nil, false, *new_args, &blk)
|
2021-02-22 17:27:13 +05:30
|
|
|
end
|
|
|
|
|
2021-04-29 21:17:54 +05:30
|
|
|
def allow_next_instances_of(klass, number, ordered = false, *new_args, &blk)
|
|
|
|
stub_new(allow(klass), number, ordered, *new_args, &blk)
|
2019-12-21 20:55:43 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2021-04-29 21:17:54 +05:30
|
|
|
def stub_new(target, number, ordered = false, *new_args, &blk)
|
2019-12-21 20:55:43 +05:30
|
|
|
receive_new = receive(:new)
|
2021-04-29 21:17:54 +05:30
|
|
|
receive_new.ordered if ordered
|
2019-12-21 20:55:43 +05:30
|
|
|
receive_new.with(*new_args) if new_args.any?
|
|
|
|
|
2022-07-16 23:28:13 +05:30
|
|
|
if number.is_a?(Range)
|
|
|
|
receive_new.at_least(number.begin).times if number.begin
|
|
|
|
receive_new.at_most(number.end).times if number.end
|
|
|
|
elsif number
|
|
|
|
receive_new.exactly(number).times
|
|
|
|
end
|
|
|
|
|
2019-12-21 20:55:43 +05:30
|
|
|
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
|