Class: Mocktail::ReplacesType

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

Instance Method Summary collapse

Constructor Details

#initializeReplacesType

Returns a new instance of ReplacesType.



9
10
11
12
13
14
# File 'lib/mocktail/replaces_type.rb', line 9

def initialize
  @top_shelf = TopShelf.instance
  @runs_sorbet_sig_blocks_before_replacement = RunsSorbetSigBlocksBeforeReplacement.new
  @redefines_new = RedefinesNew.new
  @redefines_singleton_methods = RedefinesSingletonMethods.new
end

Instance Method Details

#replace(type) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/mocktail/replaces_type.rb', line 16

def replace(type)
  unless type.is_a?(Class) || type.is_a?(Module)
    raise UnsupportedMocktail.new("Mocktail.replace() only supports classes and modules")
  end

  @runs_sorbet_sig_blocks_before_replacement.run(type)

  if type.is_a?(Class)
    @top_shelf.register_new_replacement!(type)
    @redefines_new.redefine(type)
  end

  @top_shelf.register_singleton_method_replacement!(type)
  @redefines_singleton_methods.redefine(type)
end