Class: Object
- Inherits:
- BasicObject
- Defined in:
- lib/hardmock/stubbing.rb,
lib/hardmock/stubbing.rb
Overview
Why’s “metaid.rb” stuff crunched down:
Instance Method Summary collapse
- #_clear_mock ⇒ Object
- #_is_mock? ⇒ Boolean
- #_my_name ⇒ Object
- #expects!(method_name, *args, &block) ⇒ Object
-
#hm_meta_def(name, &blk) ⇒ Object
:nodoc:#.
-
#hm_meta_eval(&blk) ⇒ Object
:nodoc:#.
-
#hm_metaclass ⇒ Object
:nodoc:#.
- #stubs!(method_name) ⇒ Object
Instance Method Details
#_clear_mock ⇒ Object
153 154 155 |
# File 'lib/hardmock/stubbing.rb', line 153 def _clear_mock @_my_mock = nil end |
#_is_mock? ⇒ Boolean
145 146 147 |
# File 'lib/hardmock/stubbing.rb', line 145 def _is_mock? self.kind_of?(Mock) end |
#_my_name ⇒ Object
149 150 151 |
# File 'lib/hardmock/stubbing.rb', line 149 def _my_name self.kind_of?(Class) ? self.name : self.class.name end |
#expects!(method_name, *args, &block) ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/hardmock/stubbing.rb', line 105 def expects!(method_name, *args, &block) if self._is_mock? raise Hardmock::StubbingError, "Cannot use 'expects!(:#{method_name})' on a Mock object; try 'expects' instead" end method_name = method_name.to_s @_my_mock = Mock.new(_my_name, $main_mock_control) if @_my_mock.nil? unless Hardmock.has_replaced_method?(self, method_name) # Track the method as replaced Hardmock::ReplacedMethod.new(self, method_name) # Preserver original implementation of the method by aliasing it away if methods.include?(method_name) do alias_method "_hardmock_original_#{method_name}".to_sym, method_name.to_sym end end # Re-define the method to utilize our patron mock instance. # (This global-temp-var thing is hokey but I was having difficulty generating # code for the meta class.) begin $method_text_temp = %{ def #{method_name}(*args,&block) @_my_mock.__send__(:#{method_name}, *args, &block) end } class << self eval $method_text_temp end ensure $method_text_temp = nil end end return @_my_mock.expects(method_name, *args, &block) end |
#hm_meta_def(name, &blk) ⇒ Object
:nodoc:#
20 21 22 |
# File 'lib/hardmock/stubbing.rb', line 20 def (name, &blk) #:nodoc:# { define_method name, &blk } end |
#hm_meta_eval(&blk) ⇒ Object
:nodoc:#
16 17 18 |
# File 'lib/hardmock/stubbing.rb', line 16 def (&blk) #:nodoc:# .instance_eval(&blk) end |
#hm_metaclass ⇒ Object
:nodoc:#
10 11 12 13 14 |
# File 'lib/hardmock/stubbing.rb', line 10 def #:nodoc:# class << self self end end |
#stubs!(method_name) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/hardmock/stubbing.rb', line 83 def stubs!(method_name) method_name = method_name.to_s already_stubbed = Hardmock.has_replaced_method?(self, method_name) stubbed_method = Hardmock::StubbedMethod.new(self, method_name) unless _is_mock? or already_stubbed if methods.include?(method_name.to_s) do alias_method "_hardmock_original_#{method_name}".to_sym, method_name.to_sym end end end method_name do |*args| stubbed_method.invoke(args) end stubbed_method end |