| Class | Mocha::Mockery |
| In: |
lib/mocha/mockery.rb
|
| Parent: | Object |
| logger | [W] |
# File lib/mocha/mockery.rb, line 81
81: def mocha_inspect
82: message = ""
83: message << "unsatisfied expectations:\n- #{unsatisfied_expectations.map { |e| e.mocha_inspect }.join("\n- ")}\n" unless unsatisfied_expectations.empty?
84: message << "satisfied expectations:\n- #{satisfied_expectations.map { |e| e.mocha_inspect }.join("\n- ")}\n" unless satisfied_expectations.empty?
85: message << "states:\n- #{state_machines.map { |sm| sm.mocha_inspect }.join("\n- ")}" unless state_machines.empty?
86: message
87: 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 89
89: def on_stubbing(object, method)
90: method = RUBY_VERSION < '1.9' ? method.to_s : method.to_sym
91: unless Mocha::Configuration.allow?(:stubbing_non_existent_method)
92: unless object.method_exists?(method, include_public_methods = true)
93: on_stubbing_non_existent_method(object, method)
94: end
95: end
96: unless Mocha::Configuration.allow?(:stubbing_non_public_method)
97: if object.method_exists?(method, include_public_methods = false)
98: on_stubbing_non_public_method(object, method)
99: end
100: end
101: unless Mocha::Configuration.allow?(:stubbing_method_on_non_mock_object)
102: on_stubbing_method_on_non_mock_object(object, method)
103: end
104: end
# File lib/mocha/mockery.rb, line 124
124: def on_stubbing_method_on_non_mock_object(object, method)
125: if Mocha::Configuration.prevent?(:stubbing_method_on_non_mock_object)
126: raise StubbingError.new("stubbing method on non-mock object: #{object.mocha_inspect}.#{method}", caller)
127: end
128: if Mocha::Configuration.warn_when?(:stubbing_method_on_non_mock_object)
129: logger.warn "stubbing method on non-mock object: #{object.mocha_inspect}.#{method}"
130: end
131: end
# File lib/mocha/mockery.rb, line 133
133: def on_stubbing_method_unnecessarily(expectation)
134: if Mocha::Configuration.prevent?(:stubbing_method_unnecessarily)
135: raise StubbingError.new("stubbing method unnecessarily: #{expectation.method_signature}", expectation.backtrace)
136: end
137: if Mocha::Configuration.warn_when?(:stubbing_method_unnecessarily)
138: logger.warn "stubbing method unnecessarily: #{expectation.method_signature}"
139: end
140: end
# File lib/mocha/mockery.rb, line 106
106: def on_stubbing_non_existent_method(object, method)
107: if Mocha::Configuration.prevent?(:stubbing_non_existent_method)
108: raise StubbingError.new("stubbing non-existent method: #{object.mocha_inspect}.#{method}", caller)
109: end
110: if Mocha::Configuration.warn_when?(:stubbing_non_existent_method)
111: logger.warn "stubbing non-existent method: #{object.mocha_inspect}.#{method}"
112: end
113: end
# File lib/mocha/mockery.rb, line 115
115: def on_stubbing_non_public_method(object, method)
116: if Mocha::Configuration.prevent?(:stubbing_non_public_method)
117: raise StubbingError.new("stubbing non-public method: #{object.mocha_inspect}.#{method}", caller)
118: end
119: if Mocha::Configuration.warn_when?(:stubbing_non_public_method)
120: logger.warn "stubbing non-public method: #{object.mocha_inspect}.#{method}"
121: end
122: 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: unless Mocha::Configuration.allow?(:stubbing_method_unnecessarily)
57: unless e.used?
58: on_stubbing_method_unnecessarily(e)
59: end
60: end
61: end
62: end
# File lib/mocha/mockery.rb, line 168
168: def add_state_machine(state_machine)
169: state_machines << state_machine
170: state_machine
171: end
# File lib/mocha/mockery.rb, line 151
151: def expectations
152: mocks.map { |mock| mock.expectations.to_a }.flatten
153: end
# File lib/mocha/mockery.rb, line 173
173: def reset
174: @stubba = nil
175: @mocks = nil
176: @state_machines = nil
177: end
# File lib/mocha/mockery.rb, line 159
159: def satisfied_expectations
160: expectations.select { |e| e.verified? }
161: end