| Class | Mocha::Mockery |
| In: |
lib/mocha/mockery.rb
|
| Parent: | Object |
| logger | [W] |
# File lib/mocha/mockery.rb, line 77
77: def mocha_inspect
78: message = ""
79: message << "unsatisfied expectations:\n- #{unsatisfied_expectations.map { |e| e.mocha_inspect }.join("\n- ")}\n" unless unsatisfied_expectations.empty?
80: message << "satisfied expectations:\n- #{satisfied_expectations.map { |e| e.mocha_inspect }.join("\n- ")}\n" unless satisfied_expectations.empty?
81: message << "states:\n- #{state_machines.map { |sm| sm.mocha_inspect }.join("\n- ")}" unless state_machines.empty?
82: message
83: end
# File lib/mocha/mockery.rb, line 33
33: def mock_impersonating(object, &block)
34: add_mock(Mock.new(ImpersonatingName.new(object), &block))
35: end
# File lib/mocha/mockery.rb, line 37
37: def mock_impersonating_any_instance_of(klass, &block)
38: add_mock(Mock.new(ImpersonatingAnyInstanceName.new(klass), &block))
39: end
# File lib/mocha/mockery.rb, line 25
25: def named_mock(name, &block)
26: add_mock(Mock.new(Name.new(name), &block))
27: end
# File lib/mocha/mockery.rb, line 41
41: def new_state_machine(name)
42: add_state_machine(StateMachine.new(name))
43: end
# File lib/mocha/mockery.rb, line 85
85: def on_stubbing(object, symbol)
86: on_stubbing_non_existent_method(object, symbol) unless object.method_exists?(symbol, include_public_methods = true)
87: on_stubbing_non_public_method(object, symbol) if object.method_exists?(symbol, include_public_methods = false)
88: on_stubbing_method_on_non_mock_object(object, symbol)
89: end
# File lib/mocha/mockery.rb, line 109
109: def on_stubbing_method_on_non_mock_object(object, symbol)
110: if Mocha::Configuration.prevent?(:stubbing_method_on_non_mock_object)
111: raise StubbingError.new("stubbing method on non-mock object: #{object.mocha_inspect}.#{symbol}", caller)
112: end
113: if Mocha::Configuration.warn_when?(:stubbing_method_on_non_mock_object)
114: logger.warn "stubbing method on non-mock object: #{object.mocha_inspect}.#{symbol}"
115: end
116: end
# File lib/mocha/mockery.rb, line 118
118: def on_stubbing_method_unnecessarily(expectation)
119: if Mocha::Configuration.prevent?(:stubbing_method_unnecessarily)
120: raise StubbingError.new("stubbing method unnecessarily: #{expectation.method_signature}", expectation.backtrace)
121: end
122: if Mocha::Configuration.warn_when?(:stubbing_method_unnecessarily)
123: logger.warn "stubbing method unnecessarily: #{expectation.method_signature}"
124: end
125: end
# File lib/mocha/mockery.rb, line 91
91: def on_stubbing_non_existent_method(object, symbol)
92: if Mocha::Configuration.prevent?(:stubbing_non_existent_method)
93: raise StubbingError.new("stubbing non-existent method: #{object.mocha_inspect}.#{symbol}", caller)
94: end
95: if Mocha::Configuration.warn_when?(:stubbing_non_existent_method)
96: logger.warn "stubbing non-existent method: #{object.mocha_inspect}.#{symbol}"
97: end
98: end
# File lib/mocha/mockery.rb, line 100
100: def on_stubbing_non_public_method(object, symbol)
101: if Mocha::Configuration.prevent?(:stubbing_non_public_method)
102: raise StubbingError.new("stubbing non-public method: #{object.mocha_inspect}.#{symbol}", caller)
103: end
104: if Mocha::Configuration.warn_when?(:stubbing_non_public_method)
105: logger.warn "stubbing non-public method: #{object.mocha_inspect}.#{symbol}"
106: end
107: end
# File lib/mocha/mockery.rb, line 29
29: def unnamed_mock(&block)
30: add_mock(Mock.new(&block))
31: end
# File lib/mocha/mockery.rb, line 45
45: def verify(assertion_counter = nil)
46: unless mocks.all? { |mock| mock.verified?(assertion_counter) }
47: message = "not all expectations were satisfied\n#{mocha_inspect}"
48: if unsatisfied_expectations.empty?
49: backtrace = caller
50: else
51: backtrace = unsatisfied_expectations[0].backtrace
52: end
53: raise ExpectationError.new(message, backtrace)
54: end
55: expectations.each do |e|
56: on_stubbing_method_unnecessarily(e) unless e.used?
57: end
58: end
# File lib/mocha/mockery.rb, line 153
153: def add_state_machine(state_machine)
154: state_machines << state_machine
155: state_machine
156: end
# File lib/mocha/mockery.rb, line 136
136: def expectations
137: mocks.map { |mock| mock.expectations.to_a }.flatten
138: end
# File lib/mocha/mockery.rb, line 158
158: def reset
159: @stubba = nil
160: @mocks = nil
161: @state_machines = nil
162: end
# File lib/mocha/mockery.rb, line 144
144: def satisfied_expectations
145: expectations.select { |e| e.verified? }
146: end