class Mocha::Mockery
def add_mock(mock)
def add_mock(mock) mocks << mock mock end
def add_state_machine(state_machine)
def add_state_machine(state_machine) state_machines << state_machine state_machine end
def expectations
def expectations mocks.map { |mock| mock.expectations.to_a }.flatten end
def instance
def instance @instance ||= new end
def logger
def logger @logger ||= Logger.new($stderr) end
def mocha_inspect
def mocha_inspect message = "" message << "unsatisfied expectations:\n- #{unsatisfied_expectations.map { |e| e.mocha_inspect }.join("\n- ")}\n" unless unsatisfied_expectations.empty? message << "satisfied expectations:\n- #{satisfied_expectations.map { |e| e.mocha_inspect }.join("\n- ")}\n" unless satisfied_expectations.empty? message << "states:\n- #{state_machines.map { |sm| sm.mocha_inspect }.join("\n- ")}" unless state_machines.empty? message end
def mock_impersonating(object, &block)
def mock_impersonating(object, &block) add_mock(Mock.new(ImpersonatingName.new(object), &block)) end
def mock_impersonating_any_instance_of(klass, &block)
def mock_impersonating_any_instance_of(klass, &block) add_mock(Mock.new(ImpersonatingAnyInstanceName.new(klass), &block)) end
def mocks
def mocks @mocks ||= [] end
def named_mock(name, &block)
def named_mock(name, &block) add_mock(Mock.new(Name.new(name), &block)) end
def new_state_machine(name)
def new_state_machine(name) add_state_machine(StateMachine.new(name)) end
def on_stubbing(object, symbol)
def on_stubbing(object, symbol) on_stubbing_non_existent_method(object, symbol) unless object.method_exists?(symbol, include_public_methods = true) on_stubbing_non_public_method(object, symbol) if object.method_exists?(symbol, include_public_methods = false) on_stubbing_method_on_non_mock_object(object, symbol) end
def on_stubbing_method_on_non_mock_object(object, symbol)
def on_stubbing_method_on_non_mock_object(object, symbol) if Mocha::Configuration.prevent?(:stubbing_method_on_non_mock_object) raise StubbingError.new("stubbing method on non-mock object: #{object.mocha_inspect}.#{symbol}", caller) end if Mocha::Configuration.warn_when?(:stubbing_method_on_non_mock_object) logger.warn "stubbing method on non-mock object: #{object.mocha_inspect}.#{symbol}" end end
def on_stubbing_method_unnecessarily(expectation)
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
def on_stubbing_non_existent_method(object, symbol)
def on_stubbing_non_existent_method(object, symbol) if Mocha::Configuration.prevent?(:stubbing_non_existent_method) raise StubbingError.new("stubbing non-existent method: #{object.mocha_inspect}.#{symbol}", caller) end if Mocha::Configuration.warn_when?(:stubbing_non_existent_method) logger.warn "stubbing non-existent method: #{object.mocha_inspect}.#{symbol}" end end
def on_stubbing_non_public_method(object, symbol)
def on_stubbing_non_public_method(object, symbol) if Mocha::Configuration.prevent?(:stubbing_non_public_method) raise StubbingError.new("stubbing non-public method: #{object.mocha_inspect}.#{symbol}", caller) end if Mocha::Configuration.warn_when?(:stubbing_non_public_method) logger.warn "stubbing non-public method: #{object.mocha_inspect}.#{symbol}" end end
def reset
def reset @stubba = nil @mocks = nil @state_machines = nil end
def reset_instance
def reset_instance @instance = nil end
def satisfied_expectations
def satisfied_expectations expectations.select { |e| e.verified? } end
def state_machines
def state_machines @state_machines ||= [] end
def stubba
def stubba @stubba ||= Central.new end
def teardown
def teardown stubba.unstub_all reset end
def unnamed_mock(&block)
def unnamed_mock(&block) add_mock(Mock.new(&block)) end
def unsatisfied_expectations
def unsatisfied_expectations expectations.reject { |e| e.verified? } end
def verify(assertion_counter = nil)
def verify(assertion_counter = nil) unless mocks.all? { |mock| mock.verified?(assertion_counter) } message = "not all expectations were satisfied\n#{mocha_inspect}" if unsatisfied_expectations.empty? backtrace = caller else backtrace = unsatisfied_expectations[0].backtrace end raise ExpectationError.new(message, backtrace) end expectations.each do |e| on_stubbing_method_unnecessarily(e) unless e.used? end end