3
3
require 'mocha/class_method'
4
4
require 'mocha/module_method'
5
5
require 'mocha/any_instance_method'
7
# Methods added all objects to allow mocking and stubbing on real objects.
9
# Methods return a Mocha::Expectation which can be further modified by methods on Mocha::Expectation.
13
@mocha ||= Mocha::Mockery.instance.mock_impersonating(self)
16
def reset_mocha # :nodoc:
20
def stubba_method # :nodoc:
24
def stubba_object # :nodoc:
28
# :call-seq: expects(symbol) -> expectation
30
# Adds an expectation that a method identified by +symbol+ must be called exactly once with any parameters.
31
# Returns the new expectation which can be further modified by methods on Mocha::Expectation.
32
# product = Product.new
33
# product.expects(:save).returns(true)
34
# assert_equal false, product.save
36
# The original implementation of <tt>Product#save</tt> is replaced temporarily.
38
# The original implementation of <tt>Product#save</tt> is restored at the end of the test.
40
mockery = Mocha::Mockery.instance
41
mockery.on_stubbing(self, symbol)
42
method = stubba_method.new(stubba_object, symbol)
43
mockery.stubba.stub(method)
44
mocha.expects(symbol, caller)
47
# :call-seq: stubs(symbol) -> expectation
49
# Adds an expectation that a method identified by +symbol+ may be called any number of times with any parameters.
50
# Returns the new expectation which can be further modified by methods on Mocha::Expectation.
51
# product = Product.new
52
# product.stubs(:save).returns(true)
53
# assert_equal false, product.save
55
# The original implementation of <tt>Product#save</tt> is replaced temporarily.
57
# The original implementation of <tt>Product#save</tt> is restored at the end of the test.
59
mockery = Mocha::Mockery.instance
60
mockery.on_stubbing(self, symbol)
61
method = stubba_method.new(stubba_object, symbol)
62
mockery.stubba.stub(method)
63
mocha.stubs(symbol, caller)
66
def method_exists?(symbol, include_public_methods = true)
67
existing_methods = private_methods(include_superclass_methods = true) + protected_methods(include_superclass_methods = true)
68
existing_methods += public_methods(include_superclass_methods = true) if include_public_methods
69
existing_methods.any? { |m| m.to_s == symbol.to_s } || (respond_to?(symbol) && include_public_methods)
6
require 'mocha/argument_iterator'
10
# Methods added all objects to allow mocking and stubbing on real objects.
12
# Methods return a Mocha::Expectation which can be further modified by methods on Mocha::Expectation.
16
@mocha ||= Mocha::Mockery.instance.mock_impersonating(self)
19
def reset_mocha # :nodoc:
23
def stubba_method # :nodoc:
27
def stubba_object # :nodoc:
31
# :call-seq: expects(method_name) -> expectation
32
# expects(method_names_vs_return_values) -> last expectation
34
# Adds an expectation that a method identified by +method_name+ Symbol must be called exactly once with any parameters.
35
# Returns the new expectation which can be further modified by methods on Mocha::Expectation.
36
# product = Product.new
37
# product.expects(:save).returns(true)
38
# assert_equal true, product.save
40
# The original implementation of <tt>Product#save</tt> is replaced temporarily.
42
# The original implementation of <tt>Product#save</tt> is restored at the end of the test.
44
# If +method_names_vs_return_values+ is a +Hash+, an expectation will be set up for each entry using the key as +method_name+ and value as +return_value+.
45
# product = Product.new
46
# product.expects(:valid? => true, :save => true)
48
# # exactly equivalent to
50
# product = Product.new
51
# product.expects(:valid?).returns(true)
52
# product.expects(:save).returns(true)
53
def expects(method_name_or_hash)
55
mockery = Mocha::Mockery.instance
56
iterator = ArgumentIterator.new(method_name_or_hash)
57
iterator.each { |*args|
58
method_name = args.shift
59
mockery.on_stubbing(self, method_name)
60
method = stubba_method.new(stubba_object, method_name)
61
mockery.stubba.stub(method)
62
expectation = mocha.expects(method_name, caller)
63
expectation.returns(args.shift) if args.length > 0
68
# :call-seq: stubs(method_name) -> expectation
69
# stubs(method_names_vs_return_values) -> last expectation
71
# Adds an expectation that a method identified by +method_name+ Symbol may be called any number of times with any parameters.
72
# Returns the new expectation which can be further modified by methods on Mocha::Expectation.
73
# product = Product.new
74
# product.stubs(:save).returns(true)
75
# assert_equal true, product.save
77
# The original implementation of <tt>Product#save</tt> is replaced temporarily.
79
# The original implementation of <tt>Product#save</tt> is restored at the end of the test.
81
# If +method_names_vs_return_values+ is a +Hash+, an expectation will be set up for each entry using the key as +method_name+ and value as +return_value+.
82
# product = Product.new
83
# product.stubs(:valid? => true, :save => true)
85
# # exactly equivalent to
87
# product = Product.new
88
# product.stubs(:valid?).returns(true)
89
# product.stubs(:save).returns(true)
90
def stubs(method_name_or_hash)
92
mockery = Mocha::Mockery.instance
93
iterator = ArgumentIterator.new(method_name_or_hash)
94
iterator.each { |*args|
95
method_name = args.shift
96
mockery.on_stubbing(self, method_name)
97
method = stubba_method.new(stubba_object, method_name)
98
mockery.stubba.stub(method)
99
expectation = mocha.stubs(method_name, caller)
100
expectation.returns(args.shift) if args.length > 0
105
def method_exists?(method, include_public_methods = true) # :nodoc:
106
if include_public_methods
107
return true if public_methods(include_superclass_methods = true).include?(method)
108
return true if respond_to?(method.to_sym)
110
return true if protected_methods(include_superclass_methods = true).include?(method)
111
return true if private_methods(include_superclass_methods = true).include?(method)
117
module ModuleMethods # :nodoc:
125
# Methods added all classes to allow mocking and stubbing on real objects.
128
def stubba_method # :nodoc:
132
class AnyInstance # :nodoc:
134
def initialize(klass)
135
@stubba_object = klass
139
@mocha ||= Mocha::Mockery.instance.mock_impersonating_any_instance_of(@stubba_object)
143
Mocha::AnyInstanceMethod
150
def method_exists?(method, include_public_methods = true)
151
if include_public_methods
152
return true if @stubba_object.public_instance_methods(include_superclass_methods = true).include?(method)
154
return true if @stubba_object.protected_instance_methods(include_superclass_methods = true).include?(method)
155
return true if @stubba_object.private_instance_methods(include_superclass_methods = true).include?(method)
161
# :call-seq: any_instance -> mock object
163
# Returns a mock object which will detect calls to any instance of this class.
164
# Product.any_instance.stubs(:save).returns(false)
165
# product_1 = Product.new
166
# assert_equal false, product_1.save
167
# product_2 = Product.new
168
# assert_equal false, product_2.save
170
@any_instance ||= AnyInstance.new(self)
177
class Object # :nodoc:
178
include Mocha::ObjectMethods
74
181
class Module # :nodoc:
84
def stubba_method # :nodoc:
88
class AnyInstance # :nodoc:
91
@stubba_object = klass
95
@mocha ||= Mocha::Mockery.instance.mock_impersonating_any_instance_of(@stubba_object)
99
Mocha::AnyInstanceMethod
106
def method_exists?(symbol, include_public_methods = true)
107
existing_methods = @stubba_object.private_instance_methods(include_superclass_methods = true) + @stubba_object.protected_instance_methods(include_superclass_methods = true)
108
existing_methods += @stubba_object.public_instance_methods(include_superclass_methods = true) if include_public_methods
109
existing_methods.any? { |m| m.to_s == symbol.to_s }
114
# :call-seq: any_instance -> mock object
116
# Returns a mock object which will detect calls to any instance of this class.
117
# Product.any_instance.stubs(:save).returns(false)
118
# product_1 = Product.new
119
# assert_equal false, product_1.save
120
# product_2 = Product.new
121
# assert_equal false, product_2.save
123
@any_instance ||= AnyInstance.new(self)
182
include Mocha::ModuleMethods
185
class Class # :nodoc:
186
include Mocha::ClassMethods