Class: Mocha::Mockery
Instance Attribute Summary collapse
Class Method Summary collapse
Instance Method Summary collapse
- #invocation(mock, method_name, args) ⇒ Object
- #invocations ⇒ Object
- #mocha_inspect ⇒ Object
- #mock_impersonating(object, &block) ⇒ Object
- #mock_impersonating_any_instance_of(klass, &block) ⇒ Object
- #mocks ⇒ Object
- #named_mock(name, &block) ⇒ Object
- #new_state_machine(name) ⇒ Object
- #on_stubbing(object, method) ⇒ Object
- #on_stubbing_method_on_non_mock_object(object, method) ⇒ Object
- #on_stubbing_method_unnecessarily(expectation) ⇒ Object
- #on_stubbing_non_existent_method(object, method) ⇒ Object
- #on_stubbing_non_public_method(object, method) ⇒ Object
- #state_machines ⇒ Object
- #stubba ⇒ Object
- #teardown ⇒ Object
- #unnamed_mock(&block) ⇒ Object
- #verify(assertion_counter = nil) ⇒ Object
Instance Attribute Details
Class Method Details
.instance ⇒ Object
16 17 18 |
# File 'lib/mocha/mockery.rb', line 16 def instance @instance ||= new end |
.reset_instance ⇒ Object
20 21 22 |
# File 'lib/mocha/mockery.rb', line 20 def reset_instance @instance = nil end |
Instance Method Details
#invocation(mock, method_name, args) ⇒ Object
46 47 48 |
# File 'lib/mocha/mockery.rb', line 46 def invocation(mock, method_name, args) invocations << Invocation.new(mock, method_name, args) end |
#invocations ⇒ Object
153 154 155 |
# File 'lib/mocha/mockery.rb', line 153 def invocations @invocations ||= [] end |
#mocha_inspect ⇒ Object
86 87 88 89 90 91 92 |
# File 'lib/mocha/mockery.rb', line 86 def mocha_inspect = "" << "unsatisfied expectations:\n- #{unsatisfied_expectations.map { |e| e.mocha_inspect }.join("\n- ")}\n" unless unsatisfied_expectations.empty? << "satisfied expectations:\n- #{satisfied_expectations.map { |e| e.mocha_inspect }.join("\n- ")}\n" unless satisfied_expectations.empty? << "states:\n- #{state_machines.map { |sm| sm.mocha_inspect }.join("\n- ")}" unless state_machines.empty? end |
#mock_impersonating(object, &block) ⇒ Object
34 35 36 |
# File 'lib/mocha/mockery.rb', line 34 def mock_impersonating(object, &block) add_mock(Mock.new(ImpersonatingName.new(object), &block)) end |
#mock_impersonating_any_instance_of(klass, &block) ⇒ Object
38 39 40 |
# File 'lib/mocha/mockery.rb', line 38 def mock_impersonating_any_instance_of(klass, &block) add_mock(Mock.new(ImpersonatingAnyInstanceName.new(klass), &block)) end |
#mocks ⇒ Object
78 79 80 |
# File 'lib/mocha/mockery.rb', line 78 def mocks @mocks ||= [] end |
#named_mock(name, &block) ⇒ Object
26 27 28 |
# File 'lib/mocha/mockery.rb', line 26 def named_mock(name, &block) add_mock(Mock.new(Name.new(name), &block)) end |
#new_state_machine(name) ⇒ Object
42 43 44 |
# File 'lib/mocha/mockery.rb', line 42 def new_state_machine(name) add_state_machine(StateMachine.new(name)) end |
#on_stubbing(object, method) ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/mocha/mockery.rb', line 94 def on_stubbing(object, method) method = RUBY_VERSION < '1.9' ? method.to_s : method.to_sym unless Mocha::Configuration.allow?(:stubbing_non_existent_method) unless object.method_exists?(method, include_public_methods = true) on_stubbing_non_existent_method(object, method) end end unless Mocha::Configuration.allow?(:stubbing_non_public_method) if object.method_exists?(method, include_public_methods = false) on_stubbing_non_public_method(object, method) end end unless Mocha::Configuration.allow?(:stubbing_method_on_non_mock_object) on_stubbing_method_on_non_mock_object(object, method) end end |
#on_stubbing_method_on_non_mock_object(object, method) ⇒ Object
129 130 131 132 133 134 135 136 |
# File 'lib/mocha/mockery.rb', line 129 def on_stubbing_method_on_non_mock_object(object, method) if Mocha::Configuration.prevent?(:stubbing_method_on_non_mock_object) raise StubbingError.new("stubbing method on non-mock object: #{object.mocha_inspect}.#{method}", caller) end if Mocha::Configuration.warn_when?(:stubbing_method_on_non_mock_object) logger.warn "stubbing method on non-mock object: #{object.mocha_inspect}.#{method}" end end |
#on_stubbing_method_unnecessarily(expectation) ⇒ Object
138 139 140 141 142 143 144 145 |
# File 'lib/mocha/mockery.rb', line 138 def on_stubbing_method_unnecessarily(expectation) if Mocha::Configuration.prevent?(:stubbing_method_unnecessarily) raise StubbingError.new("stubbing method unnecessarily: #{expectation.method_signature}", expectation.backtrace) end if Mocha::Configuration.warn_when?(:stubbing_method_unnecessarily) logger.warn "stubbing method unnecessarily: #{expectation.method_signature}" end end |
#on_stubbing_non_existent_method(object, method) ⇒ Object
111 112 113 114 115 116 117 118 |
# File 'lib/mocha/mockery.rb', line 111 def on_stubbing_non_existent_method(object, method) if Mocha::Configuration.prevent?(:stubbing_non_existent_method) raise StubbingError.new("stubbing non-existent method: #{object.mocha_inspect}.#{method}", caller) end if Mocha::Configuration.warn_when?(:stubbing_non_existent_method) logger.warn "stubbing non-existent method: #{object.mocha_inspect}.#{method}" end end |
#on_stubbing_non_public_method(object, method) ⇒ Object
120 121 122 123 124 125 126 127 |
# File 'lib/mocha/mockery.rb', line 120 def on_stubbing_non_public_method(object, method) if Mocha::Configuration.prevent?(:stubbing_non_public_method) raise StubbingError.new("stubbing non-public method: #{object.mocha_inspect}.#{method}", caller) end if Mocha::Configuration.warn_when?(:stubbing_non_public_method) logger.warn "stubbing non-public method: #{object.mocha_inspect}.#{method}" end end |
#state_machines ⇒ Object
82 83 84 |
# File 'lib/mocha/mockery.rb', line 82 def state_machines @state_machines ||= [] end |
#stubba ⇒ Object
74 75 76 |
# File 'lib/mocha/mockery.rb', line 74 def stubba @stubba ||= Central.new end |
#teardown ⇒ Object
69 70 71 72 |
# File 'lib/mocha/mockery.rb', line 69 def teardown stubba.unstub_all reset end |
#unnamed_mock(&block) ⇒ Object
30 31 32 |
# File 'lib/mocha/mockery.rb', line 30 def unnamed_mock(&block) add_mock(Mock.new(&block)) end |
#verify(assertion_counter = nil) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/mocha/mockery.rb', line 50 def verify(assertion_counter = nil) unless mocks.all? { |mock| mock.__verified__?(assertion_counter) } = "not all expectations were satisfied\n#{mocha_inspect}" if unsatisfied_expectations.empty? backtrace = caller else backtrace = unsatisfied_expectations[0].backtrace end raise ExpectationError.new(, backtrace) end expectations.each do |e| unless Mocha::Configuration.allow?(:stubbing_method_unnecessarily) unless e.used? on_stubbing_method_unnecessarily(e) end end end end |