Module: Mocha::ObjectMethods

Included in:
Object
Defined in:
lib/mocha/object.rb,
lib/mocha/inspect.rb,
lib/mocha/parameter_matchers/object.rb

Overview

Methods added to all objects to allow mocking and stubbing on real (i.e. non-mock) objects.

Both #expects and #stubs return an Expectation which can be further modified by methods on Expectation.

Instance Method Summary collapse

Instance Method Details

#expects(method_name) ⇒ Expectation #expects(expected_methods_vs_return_values) ⇒ Expectation

Adds an expectation that the specified method must be called exactly once with any parameters.

The original implementation of the method is replaced during the test and then restored at the end of the test.

Examples:

Setting up an expectation on a non-mock object.

product = Product.new
product.expects(:save).returns(true)
assert_equal true, product.save

Setting up multiple expectations on a non-mock object.

product = Product.new
product.expects(:valid? => true, :save => true)

# exactly equivalent to

product = Product.new
product.expects(:valid?).returns(true)
product.expects(:save).returns(true)

Raises:

  • (StubbingError)

    if attempting to stub method which is not allowed.

See Also:


66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/mocha/object.rb', line 66

def expects(expected_methods_vs_return_values)
  if expected_methods_vs_return_values.to_s =~ /the[^a-z]*spanish[^a-z]*inquisition/i
    raise Mocha::ExpectationError.new('NOBODY EXPECTS THE SPANISH INQUISITION!')
  end
  if frozen?
    raise StubbingError.new("can't stub method on frozen object: #{mocha_inspect}", caller)
  end
  expectation = nil
  mockery = Mocha::Mockery.instance
  iterator = ArgumentIterator.new(expected_methods_vs_return_values)
  iterator.each { |*args|
    method_name = args.shift
    mockery.on_stubbing(self, method_name)
    method = stubba_method.new(stubba_object, method_name)
    mockery.stubba.stub(method)
    expectation = mocha.expects(method_name, caller)
    expectation.returns(args.shift) if args.length > 0
  }
  expectation
end

#method_exists?(method, include_public_methods = true) ⇒ Boolean


163
164
165
166
167
168
169
170
171
# File 'lib/mocha/object.rb', line 163

def method_exists?(method, include_public_methods = true)
  if include_public_methods
    return true if public_methods(include_superclass_methods = true).include?(method)
    return true if respond_to?(method.to_sym)
  end
  return true if protected_methods(include_superclass_methods = true).include?(method)
  return true if private_methods(include_superclass_methods = true).include?(method)
  return false
end

#mochaObject


19
20
21
# File 'lib/mocha/object.rb', line 19

def mocha
  @mocha ||= Mocha::Mockery.instance.mock_impersonating(self)
end

#mocha_inspectObject


6
7
8
9
10
# File 'lib/mocha/inspect.rb', line 6

def mocha_inspect
  address = self.__id__ * 2
  address += 0x100000000 if address < 0
  inspect =~ /#</ ? "#<#{self.class}:0x#{'%x' % address}>" : inspect
end

#reset_mochaObject


24
25
26
# File 'lib/mocha/object.rb', line 24

def reset_mocha
  @mocha = nil
end

#stubba_methodObject


29
30
31
# File 'lib/mocha/object.rb', line 29

def stubba_method
  Mocha::InstanceMethod
end

#stubba_objectObject


34
35
36
# File 'lib/mocha/object.rb', line 34

def stubba_object
  self
end

#stubs(method_name) ⇒ Expectation #stubs(stubbed_methods_vs_return_values) ⇒ Expectation

Adds an expectation that the specified method may be called any number of times with any parameters.

Examples:

Setting up a stubbed methods on a non-mock object.

product = Product.new
product.stubs(:save).returns(true)
assert_equal true, product.save

Setting up multiple stubbed methods on a non-mock object.

product = Product.new
product.stubs(:valid? => true, :save => true)

# exactly equivalent to

product = Product.new
product.stubs(:valid?).returns(true)
product.stubs(:save).returns(true)

Raises:

  • (StubbingError)

    if attempting to stub method which is not allowed.

See Also:


113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/mocha/object.rb', line 113

def stubs(stubbed_methods_vs_return_values)
  if frozen?
    raise StubbingError.new("can't stub method on frozen object: #{mocha_inspect}", caller)
  end
  expectation = nil
  mockery = Mocha::Mockery.instance
  iterator = ArgumentIterator.new(stubbed_methods_vs_return_values)
  iterator.each { |*args|
    method_name = args.shift
    mockery.on_stubbing(self, method_name)
    method = stubba_method.new(stubba_object, method_name)
    mockery.stubba.stub(method)
    expectation = mocha.stubs(method_name, caller)
    expectation.returns(args.shift) if args.length > 0
  }
  expectation
end

#to_matcherObject


7
8
9
# File 'lib/mocha/parameter_matchers/object.rb', line 7

def to_matcher
  Mocha::ParameterMatchers::Equals.new(self)
end

#unstub(*method_names) ⇒ Object

Removes the specified stubbed methods (added by calls to #expects or #stubs) and all expectations associated with them.

Restores the original behaviour of the methods before they were stubbed.

WARNING: If you #unstub a method which still has unsatisfied expectations, you may be removing the only way those expectations can be satisfied. Use #unstub with care.

Examples:

Stubbing and unstubbing a method on a real (non-mock) object.

multiplier = Multiplier.new
multiplier.double(2) # => 4
multiplier.stubs(:double).raises # new behaviour defined
multiplier.double(2) # => raises exception
multiplier.unstub(:double) # original behaviour restored
multiplier.double(2) # => 4

Unstubbing multiple methods on a real (non-mock) object.

multiplier.unstub(:double, :triple)

# exactly equivalent to

multiplier.unstub(:double)
multiplier.unstub(:triple)

154
155
156
157
158
159
160
# File 'lib/mocha/object.rb', line 154

def unstub(*method_names)
  mockery = Mocha::Mockery.instance
  method_names.each do |method_name|
    method = stubba_method.new(stubba_object, method_name)
    mockery.stubba.unstub(method)
  end
end