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

Instance Method Details

#_clear_mockObject



153
154
155
# File 'lib/hardmock/stubbing.rb', line 153

def _clear_mock
  @_my_mock = nil
end

#_is_mock?Boolean

Returns:

  • (Boolean)


145
146
147
# File 'lib/hardmock/stubbing.rb', line 145

def _is_mock?
  self.kind_of?(Mock)
end

#_my_nameObject



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)
      hm_meta_eval 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 hm_meta_def(name, &blk) #:nodoc:#
	hm_meta_eval { define_method name, &blk }
end

#hm_meta_eval(&blk) ⇒ Object

:nodoc:#



16
17
18
# File 'lib/hardmock/stubbing.rb', line 16

def hm_meta_eval(&blk) #:nodoc:#
  hm_metaclass.instance_eval(&blk)
end

#hm_metaclassObject

:nodoc:#



10
11
12
13
14
# File 'lib/hardmock/stubbing.rb', line 10

def hm_metaclass #: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)
      hm_meta_eval do 
        alias_method "_hardmock_original_#{method_name}".to_sym, method_name.to_sym
      end
    end
  end

  hm_meta_def method_name do |*args|
    stubbed_method.invoke(args)
  end

  stubbed_method
end