2019-10-11 17:05:59 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module NextInstanceOf
|
2020-12-01 01:09:37 -05:00
|
|
|
def expect_next_instance_of(klass, *new_args, &blk)
|
2021-03-26 05:09:24 -04:00
|
|
|
stub_new(expect(klass), nil, false, *new_args, &blk)
|
2020-12-09 16:09:43 -05:00
|
|
|
end
|
|
|
|
|
2021-03-26 05:09:24 -04:00
|
|
|
def expect_next_instances_of(klass, number, ordered = false, *new_args, &blk)
|
|
|
|
stub_new(expect(klass), number, ordered, *new_args, &blk)
|
2019-10-11 17:05:59 -04:00
|
|
|
end
|
|
|
|
|
2020-12-01 01:09:37 -05:00
|
|
|
def allow_next_instance_of(klass, *new_args, &blk)
|
2021-03-26 05:09:24 -04:00
|
|
|
stub_new(allow(klass), nil, false, *new_args, &blk)
|
2020-12-09 16:09:43 -05:00
|
|
|
end
|
|
|
|
|
2021-03-26 05:09:24 -04:00
|
|
|
def allow_next_instances_of(klass, number, ordered = false, *new_args, &blk)
|
|
|
|
stub_new(allow(klass), number, ordered, *new_args, &blk)
|
2019-10-11 17:05:59 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2021-03-26 05:09:24 -04:00
|
|
|
def stub_new(target, number, ordered = false, *new_args, &blk)
|
2019-10-11 17:05:59 -04:00
|
|
|
receive_new = receive(:new)
|
2021-03-26 05:09:24 -04:00
|
|
|
receive_new.ordered if ordered
|
2022-06-07 11:08:12 -04:00
|
|
|
receive_new.with(*new_args) if new_args.present?
|
2019-10-11 17:05:59 -04:00
|
|
|
|
2022-05-17 11:09:01 -04:00
|
|
|
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-10-11 17:05:59 -04:00
|
|
|
target.to receive_new.and_wrap_original do |method, *original_args|
|
2020-12-01 01:09:37 -05:00
|
|
|
method.call(*original_args).tap(&blk)
|
2019-10-11 17:05:59 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|