Class: Mocha::Mock

Inherits:
Object show all
Includes:
MockMethods
Defined in:
lib/mocha/mock.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from MockMethods

#expectations, #expectations_matching, #matching_expectation, #method_missing, #stubs, #super_method_missing, #unexpected_method_called, #verify

Constructor Details

#initialize(*arguments) ⇒ Mock

Returns a new instance of Mock.



11
12
13
14
15
16
17
18
# File 'lib/mocha/mock.rb', line 11

def initialize(*arguments)
  @mocked = arguments.shift unless arguments.first.is_a?(Hash)
  @mocked ||= always_responds
  expectations = arguments.shift || {}
  expectations.each do |method_name, result|
    expects(method_name).returns(result)
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Mocha::MockMethods

Instance Attribute Details

#mockedObject (readonly)

Returns the value of attribute mocked.



9
10
11
# File 'lib/mocha/mock.rb', line 9

def mocked
  @mocked
end

Instance Method Details

#always_respondsObject



25
26
27
# File 'lib/mocha/mock.rb', line 25

def always_responds
  Class.new { def respond_to?(symbol); true; end }.new
end

#expects(symbol) ⇒ Object

Raises:

  • (Test::Unit::AssertionFailedError)


20
21
22
23
# File 'lib/mocha/mock.rb', line 20

def expects(symbol)
  raise Test::Unit::AssertionFailedError, "Cannot replace #{symbol} as #{@mocked} does not respond to it." unless @mocked.respond_to?(symbol)
  super
end