class Mocha::Mockery
Attributes
logger[W]
Public Class Methods
instance()
click to toggle source
# File lib/mocha/mockery.rb, line 37 def instance instances.last || Null.new end
setup()
click to toggle source
# File lib/mocha/mockery.rb, line 41 def setup mockery = new mockery.logger = instance.logger unless instances.empty? @instances.push(mockery) end
teardown()
click to toggle source
# File lib/mocha/mockery.rb, line 51 def teardown instance.teardown ensure @instances.pop @instances = nil if instances.empty? end
verify(*args)
click to toggle source
# File lib/mocha/mockery.rb, line 47 def verify(*args) instance.verify(*args) end
Private Class Methods
instances()
click to toggle source
# File lib/mocha/mockery.rb, line 60 def instances @instances ||= [] end
Public Instance Methods
logger()
click to toggle source
# File lib/mocha/mockery.rb, line 191 def logger @logger ||= Logger.new($stderr) end
mocha_inspect()
click to toggle source
# File lib/mocha/mockery.rb, line 120 def mocha_inspect message = '' message << "unsatisfied expectations:\n- #{unsatisfied_expectations.map(&:mocha_inspect).join("\n- ")}\n" unless unsatisfied_expectations.empty? message << "satisfied expectations:\n- #{satisfied_expectations.map(&:mocha_inspect).join("\n- ")}\n" unless satisfied_expectations.empty? message << "states:\n- #{state_machines.map(&:mocha_inspect).join("\n- ")}" unless state_machines.empty? message end
mock_impersonating(object, &block)
click to toggle source
# File lib/mocha/mockery.rb, line 73 def mock_impersonating(object, &block) add_mock(Mock.new(self, ImpersonatingName.new(object), ObjectReceiver.new(object), &block)) end
mock_impersonating_any_instance_of(klass, &block)
click to toggle source
# File lib/mocha/mockery.rb, line 77 def mock_impersonating_any_instance_of(klass, &block) add_mock(Mock.new(self, ImpersonatingAnyInstanceName.new(klass), AnyInstanceReceiver.new(klass), &block)) end
mocks()
click to toggle source
# File lib/mocha/mockery.rb, line 112 def mocks @mocks ||= [] end
named_mock(name, &block)
click to toggle source
# File lib/mocha/mockery.rb, line 65 def named_mock(name, &block) add_mock(Mock.new(self, Name.new(name), &block)) end
new_state_machine(name)
click to toggle source
# File lib/mocha/mockery.rb, line 81 def new_state_machine(name) add_state_machine(StateMachine.new(name)) end
on_stubbing(object, method)
click to toggle source
# File lib/mocha/mockery.rb, line 128 def on_stubbing(object, method) method = PRE_RUBY_V19 ? method.to_s : method.to_sym unless Mocha::Configuration.allow?(:stubbing_non_existent_method) unless object.method_exists?(method, true) on_stubbing_non_existent_method(object, method) end end unless Mocha::Configuration.allow?(:stubbing_non_public_method) if object.method_exists?(method, false) on_stubbing_non_public_method(object, method) end end unless Mocha::Configuration.allow?(:stubbing_method_on_nil) if object.nil? on_stubbing_method_on_nil(object, method) end end return if Mocha::Configuration.allow?(:stubbing_method_on_non_mock_object) on_stubbing_method_on_non_mock_object(object, method) end
on_stubbing_method_on_nil(object, method)
click to toggle source
# File lib/mocha/mockery.rb, line 165 def on_stubbing_method_on_nil(object, method) if Mocha::Configuration.prevent?(:stubbing_method_on_nil) raise StubbingError.new("stubbing method on nil: #{object.mocha_inspect}.#{method}", caller) end return unless Mocha::Configuration.warn_when?(:stubbing_method_on_nil) logger.warn "stubbing method on nil: #{object.mocha_inspect}.#{method}" end
on_stubbing_method_on_non_mock_object(object, method)
click to toggle source
# File lib/mocha/mockery.rb, line 173 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 return unless Mocha::Configuration.warn_when?(:stubbing_method_on_non_mock_object) logger.warn "stubbing method on non-mock object: #{object.mocha_inspect}.#{method}" end
on_stubbing_method_unnecessarily(expectation)
click to toggle source
# File lib/mocha/mockery.rb, line 181 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 return unless Mocha::Configuration.warn_when?(:stubbing_method_unnecessarily) logger.warn "stubbing method unnecessarily: #{expectation.method_signature}" end
on_stubbing_non_existent_method(object, method)
click to toggle source
# File lib/mocha/mockery.rb, line 149 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 return unless Mocha::Configuration.warn_when?(:stubbing_non_existent_method) logger.warn "stubbing non-existent method: #{object.mocha_inspect}.#{method}" end
on_stubbing_non_public_method(object, method)
click to toggle source
# File lib/mocha/mockery.rb, line 157 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 return unless Mocha::Configuration.warn_when?(:stubbing_non_public_method) logger.warn "stubbing non-public method: #{object.mocha_inspect}.#{method}" end
state_machines()
click to toggle source
# File lib/mocha/mockery.rb, line 116 def state_machines @state_machines ||= [] end
stubba()
click to toggle source
# File lib/mocha/mockery.rb, line 108 def stubba @stubba ||= Central.new end
teardown()
click to toggle source
# File lib/mocha/mockery.rb, line 103 def teardown stubba.unstub_all reset end
unnamed_mock(&block)
click to toggle source
# File lib/mocha/mockery.rb, line 69 def unnamed_mock(&block) add_mock(Mock.new(self, &block)) end
verify(assertion_counter = nil)
click to toggle source
# File lib/mocha/mockery.rb, line 85 def verify(assertion_counter = nil) unless mocks.all? { |mock| mock.__verified__?(assertion_counter) } message = "not all expectations were satisfied\n#{mocha_inspect}" backtrace = if unsatisfied_expectations.empty? caller else unsatisfied_expectations[0].backtrace end raise ExpectationErrorFactory.build(message, backtrace) end expectations.each do |e| unless Mocha::Configuration.allow?(:stubbing_method_unnecessarily) next if e.used? on_stubbing_method_unnecessarily(e) end end end
Private Instance Methods
add_mock(mock)
click to toggle source
# File lib/mocha/mockery.rb, line 209 def add_mock(mock) mocks << mock mock end
add_state_machine(state_machine)
click to toggle source
# File lib/mocha/mockery.rb, line 214 def add_state_machine(state_machine) state_machines << state_machine state_machine end
expectations()
click to toggle source
# File lib/mocha/mockery.rb, line 197 def expectations mocks.map { |mock| mock.__expectations__.to_a }.flatten end
reset()
click to toggle source
# File lib/mocha/mockery.rb, line 219 def reset @stubba = nil @mocks = nil @state_machines = nil end
satisfied_expectations()
click to toggle source
# File lib/mocha/mockery.rb, line 205 def satisfied_expectations expectations.select(&:verified?) end
unsatisfied_expectations()
click to toggle source
# File lib/mocha/mockery.rb, line 201 def unsatisfied_expectations expectations.reject(&:verified?) end