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
|
2020-12-09 16:09:43 -05:00
|
|
|
receive_new.exactly(number).times if number
|
2019-10-11 17:05:59 -04:00
|
|
|
receive_new.with(*new_args) if new_args.any?
|
|
|
|
|
|
|
|
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
|