class Mocha::Mockery

Attributes

logger[W]

Public Class Methods

instance() click to toggle source
# File lib/mocha/mockery.rb, line 36
def instance
  @instances.last || Null.new
end
setup() click to toggle source
# File lib/mocha/mockery.rb, line 40
def setup
  @instances ||= []
  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
end
verify(*args) click to toggle source
# File lib/mocha/mockery.rb, line 47
def verify(*args)
  instance.verify(*args)
end

Public Instance Methods

logger() click to toggle source
# File lib/mocha/mockery.rb, line 134
def logger
  @logger ||= Logger.new($stderr)
end
mocha_inspect() click to toggle source
# File lib/mocha/mockery.rb, line 112
def mocha_inspect
  message = ''
  message << "unsatisfied expectations:\n- #{unsatisfied_expectations.map(&:mocha_inspect).join("\n- ")}\n" if unsatisfied_expectations.any?
  message << "satisfied expectations:\n- #{satisfied_expectations.map(&:mocha_inspect).join("\n- ")}\n" if satisfied_expectations.any?
  message << "states:\n- #{state_machines.map(&:mocha_inspect).join("\n- ")}\n" if state_machines.any?
  message
end
mock_impersonating(object) click to toggle source
# File lib/mocha/mockery.rb, line 66
def mock_impersonating(object)
  add_mock(Mock.new(self, ImpersonatingName.new(object), ObjectReceiver.new(object)))
end
mock_impersonating_any_instance_of(klass) click to toggle source
# File lib/mocha/mockery.rb, line 70
def mock_impersonating_any_instance_of(klass)
  add_mock(Mock.new(self, ImpersonatingAnyInstanceName.new(klass), AnyInstanceReceiver.new(klass)))
end
mocks() click to toggle source
# File lib/mocha/mockery.rb, line 104
def mocks
  @mocks ||= []
end
named_mock(name) click to toggle source
# File lib/mocha/mockery.rb, line 58
def named_mock(name)
  add_mock(Mock.new(self, Name.new(name)))
end
new_state_machine(name) click to toggle source
# File lib/mocha/mockery.rb, line 74
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 120
def on_stubbing(object, method)
  signature_proc = lambda { "#{object.mocha_inspect}.#{method}" }
  check(:stubbing_non_existent_method, 'non-existent method', signature_proc) do
    !(object.stubba_class.__method_exists__?(method, true) || object.respond_to?(method))
  end
  check(:stubbing_non_public_method, 'non-public method', signature_proc) do
    object.stubba_class.__method_exists__?(method, false)
  end
  check(:stubbing_method_on_nil, 'method on nil', signature_proc) { object.nil? }
  check(:stubbing_method_on_non_mock_object, 'method on non-mock object', signature_proc)
end
state_machines() click to toggle source
# File lib/mocha/mockery.rb, line 108
def state_machines
  @state_machines ||= []
end
stubba() click to toggle source
# File lib/mocha/mockery.rb, line 100
def stubba
  @stubba ||= Central.new
end
teardown() click to toggle source
# File lib/mocha/mockery.rb, line 94
def teardown
  stubba.unstub_all
  mocks.each(&:__expire__)
  reset
end
unnamed_mock() click to toggle source
# File lib/mocha/mockery.rb, line 62
def unnamed_mock
  add_mock(Mock.new(self))
end
verify(assertion_counter = nil) click to toggle source
# File lib/mocha/mockery.rb, line 78
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.reject(&:used?).each do |expectation|
    signature_proc = lambda { expectation.method_signature }
    check(:stubbing_method_unnecessarily, 'method unnecessarily', signature_proc, expectation.backtrace)
  end
end

Private Instance Methods

add_mock(mock) click to toggle source
# File lib/mocha/mockery.rb, line 161
def add_mock(mock)
  mocks << mock
  mock
end
add_state_machine(state_machine) click to toggle source
# File lib/mocha/mockery.rb, line 166
def add_state_machine(state_machine)
  state_machines << state_machine
  state_machine
end
check(action, description, signature_proc, backtrace = caller) { || ... } click to toggle source
# File lib/mocha/mockery.rb, line 140
def check(action, description, signature_proc, backtrace = caller)
  treatment = Mocha.configuration.send(action)
  return if (treatment == :allow) || (block_given? && !yield)
  method_signature = signature_proc.call
  message = "stubbing #{description}: #{method_signature}"
  raise StubbingError.new(message, backtrace) if treatment == :prevent
  logger.warn(message) if treatment == :warn
end
expectations() click to toggle source
# File lib/mocha/mockery.rb, line 149
def expectations
  mocks.map { |mock| mock.__expectations__.to_a }.flatten
end
reset() click to toggle source
# File lib/mocha/mockery.rb, line 171
def reset
  @stubba = nil
  @mocks = nil
  @state_machines = nil
end
satisfied_expectations() click to toggle source
# File lib/mocha/mockery.rb, line 157
def satisfied_expectations
  expectations.select(&:verified?)
end
unsatisfied_expectations() click to toggle source
# File lib/mocha/mockery.rb, line 153
def unsatisfied_expectations
  expectations.reject(&:verified?)
end