Class: Object

Inherits:
BasicObject
Defined in:
lib/defekt/object.rb

Instance Method Summary collapse

Instance Method Details

#stub(methot, value) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/defekt/object.rb', line 2

def stub(methot, value)
  singleton_methot_exists = respond_to?(methot) && singleton_methods.include?(methot)

  if singleton_methot_exists
    original = "__original_#{methot}__"
    singleton_class.class_eval { alias_method(original, methot) }
  end

  define_singleton_method(methot) { |*arguments| value }

  yield
ensure
  if singleton_methot_exists
    singleton_class.class_eval do
      alias_method(methot, original)
      remove_method(original)
    end
  else
    singleton_class.class_eval { remove_method(methot) }
  end
end