Class: Mocktail::ReplacesNext

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/mocktail/replaces_next.rb,
lib/mocktail/sorbet/mocktail/replaces_next.rb

Instance Method Summary collapse

Constructor Details

#initializeReplacesNext

Returns a new instance of ReplacesNext.



5
6
7
8
9
# File 'lib/mocktail/replaces_next.rb', line 5

def initialize
  @top_shelf = TopShelf.instance
  @redefines_new = RedefinesNew.new
  @imitates_type = ImitatesType.new
end

Instance Method Details

#replace(type, count) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/mocktail/replaces_next.rb', line 15

def replace(type, count)
  raise UnsupportedMocktail.new("Mocktail.of_next() only supports classes") unless type.is_a?(Class)

  mocktails = count.times.map { @imitates_type.imitate(type) }

  @top_shelf.register_of_next_replacement!(type)
  @redefines_new.redefine(type)
  mocktails.reverse_each do |mocktail|
    Mocktail.stubs(
      ignore_extra_args: true,
      ignore_block: true,
      ignore_arity: true,
      times: 1
    ) {
      type.new
    }.with {
      if mocktail == mocktails.last
        @top_shelf.unregister_of_next_replacement!(type)
      end

      mocktail
    }
  end

  mocktails
end

#replace_once(type) ⇒ Object



11
12
13
# File 'lib/mocktail/replaces_next.rb', line 11

def replace_once(type)
  replace(type, 1).fetch(0)
end