File: object.rb

package info (click to toggle)
libmocha-ruby 0.9.0-1
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 944 kB
  • ctags: 1,384
  • sloc: ruby: 7,265; makefile: 4
file content (127 lines) | stat: -rw-r--r-- 3,921 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
require 'mocha/mockery'
require 'mocha/instance_method'
require 'mocha/class_method'
require 'mocha/module_method'
require 'mocha/any_instance_method'

# Methods added all objects to allow mocking and stubbing on real objects.
#
# Methods return a Mocha::Expectation which can be further modified by methods on Mocha::Expectation.
class Object
  
  def mocha # :nodoc:
    @mocha ||= Mocha::Mockery.instance.mock_impersonating(self)
  end
  
  def reset_mocha # :nodoc:
    @mocha = nil
  end
  
  def stubba_method # :nodoc:
    Mocha::InstanceMethod
  end
  
  def stubba_object # :nodoc:
    self
  end
  
  # :call-seq: expects(symbol) -> expectation
  #
  # Adds an expectation that a method identified by +symbol+ must be called exactly once with any parameters.
  # Returns the new expectation which can be further modified by methods on Mocha::Expectation.
  #   product = Product.new
  #   product.expects(:save).returns(true)
  #   assert_equal false, product.save
  #
  # The original implementation of <tt>Product#save</tt> is replaced temporarily.
  #
  # The original implementation of <tt>Product#save</tt> is restored at the end of the test.
  def expects(symbol)
    mockery = Mocha::Mockery.instance
    mockery.on_stubbing(self, symbol)
    method = stubba_method.new(stubba_object, symbol)
    mockery.stubba.stub(method)
    mocha.expects(symbol, caller)
  end
  
  # :call-seq: stubs(symbol) -> expectation
  #
  # Adds an expectation that a method identified by +symbol+ may be called any number of times with any parameters.
  # Returns the new expectation which can be further modified by methods on Mocha::Expectation.
  #   product = Product.new
  #   product.stubs(:save).returns(true)
  #   assert_equal false, product.save
  #
  # The original implementation of <tt>Product#save</tt> is replaced temporarily.
  #
  # The original implementation of <tt>Product#save</tt> is restored at the end of the test.
  def stubs(symbol)
    mockery = Mocha::Mockery.instance
    mockery.on_stubbing(self, symbol)
    method = stubba_method.new(stubba_object, symbol)
    mockery.stubba.stub(method)
    mocha.stubs(symbol, caller)
  end
  
  def method_exists?(symbol, include_public_methods = true)
    existing_methods = private_methods(include_superclass_methods = true) + protected_methods(include_superclass_methods = true)
    existing_methods += public_methods(include_superclass_methods = true) if include_public_methods
    existing_methods.any? { |m| m.to_s == symbol.to_s } || (respond_to?(symbol) && include_public_methods)
  end
  
end

class Module # :nodoc:
  
  def stubba_method
    Mocha::ModuleMethod
  end
    
end
  
class Class
  
  def stubba_method # :nodoc:
    Mocha::ClassMethod
  end

  class AnyInstance # :nodoc:
    
    def initialize(klass)
      @stubba_object = klass
    end
    
    def mocha
      @mocha ||= Mocha::Mockery.instance.mock_impersonating_any_instance_of(@stubba_object)
    end

    def stubba_method
      Mocha::AnyInstanceMethod
    end
    
    def stubba_object
      @stubba_object
    end
    
    def method_exists?(symbol, include_public_methods = true)
      existing_methods = @stubba_object.private_instance_methods(include_superclass_methods = true) + @stubba_object.protected_instance_methods(include_superclass_methods = true)
      existing_methods += @stubba_object.public_instance_methods(include_superclass_methods = true) if include_public_methods
      existing_methods.any? { |m| m.to_s == symbol.to_s }
    end
    
  end
  
  # :call-seq: any_instance -> mock object
  #
  # Returns a mock object which will detect calls to any instance of this class.
  #   Product.any_instance.stubs(:save).returns(false)
  #   product_1 = Product.new
  #   assert_equal false, product_1.save
  #   product_2 = Product.new
  #   assert_equal false, product_2.save
  def any_instance
    @any_instance ||= AnyInstance.new(self)
  end
  
end