Module: Mocha::MockMethods

Included in:
Mock, MockClass, MockClass
Defined in:
lib/mocha/mock_methods.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(symbol, *arguments, &block) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/mocha/mock_methods.rb', line 22

def method_missing(symbol, *arguments, &block)
   matching_expectation = matching_expectation(symbol, *arguments)
   if matching_expectation then
     matching_expectation.invoke(&block)
   elsif stub_everything then
     return
   else
     begin
       super_method_missing(symbol, *arguments, &block)
 		rescue NoMethodError
 			unexpected_method_called(symbol, *arguments)
 		end
	end
end

Instance Attribute Details

#stub_everythingObject (readonly)

Returns the value of attribute stub_everything.



6
7
8
# File 'lib/mocha/mock_methods.rb', line 6

def stub_everything
  @stub_everything
end

Instance Method Details

#expectationsObject



8
9
10
# File 'lib/mocha/mock_methods.rb', line 8

def expectations
  @expectations ||= []
end

#expects(symbol, backtrace = nil) ⇒ Object



12
13
14
15
# File 'lib/mocha/mock_methods.rb', line 12

def expects(symbol, backtrace = nil)
  expectations << Expectation.new(symbol, backtrace)
  expectations.last
end

#matching_expectation(symbol, *arguments) ⇒ Object



49
50
51
# File 'lib/mocha/mock_methods.rb', line 49

def matching_expectation(symbol, *arguments)
  expectations.detect { |expectation| expectation.match?(symbol, *arguments) }
end

#respond_to?(symbol) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/mocha/mock_methods.rb', line 37

def respond_to?(symbol)
  expectations.any? { |expectation| expectation.method_name == symbol }
end

#stubs(symbol, backtrace = nil) ⇒ Object



17
18
19
20
# File 'lib/mocha/mock_methods.rb', line 17

def stubs(symbol, backtrace = nil)
  expectations << Stub.new(symbol, backtrace)
  expectations.last
end

#super_method_missing(symbol, *arguments, &block) ⇒ Object

Raises:

  • (NoMethodError)


41
42
43
# File 'lib/mocha/mock_methods.rb', line 41

def super_method_missing(symbol, *arguments, &block)
 raise NoMethodError
end

#unexpected_method_called(symbol, *arguments) ⇒ Object



45
46
47
# File 'lib/mocha/mock_methods.rb', line 45

def unexpected_method_called(symbol, *arguments)
  MissingExpectation.new(symbol, expectations).with(*arguments).verify
end

#verify(&block) ⇒ Object



53
54
55
# File 'lib/mocha/mock_methods.rb', line 53

def verify(&block)
  expectations.each { |expectation| expectation.verify(&block) }
end