fix Mock class: child of object class and fix realClass usage
This commit is contained in:
parent
a0d0a9b56c
commit
3d559033cb
|
@ -11,6 +11,7 @@
|
||||||
#
|
#
|
||||||
#
|
#
|
||||||
# Copyright (c) 2005, Dave Kirby
|
# Copyright (c) 2005, Dave Kirby
|
||||||
|
# Copyright (c) 2009, Yann Leboulanger
|
||||||
#
|
#
|
||||||
# All rights reserved.
|
# All rights reserved.
|
||||||
#
|
#
|
||||||
|
@ -60,7 +61,7 @@ import re
|
||||||
class MockInterfaceError(Exception):
|
class MockInterfaceError(Exception):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
class Mock:
|
class Mock(object):
|
||||||
"""
|
"""
|
||||||
The Mock class emulates any other class for testing purposes.
|
The Mock class emulates any other class for testing purposes.
|
||||||
All method calls are stored for later examination.
|
All method calls are stored for later examination.
|
||||||
|
@ -83,6 +84,7 @@ class Mock:
|
||||||
self.mockAllCalledMethods = []
|
self.mockAllCalledMethods = []
|
||||||
self.mockReturnValues = returnValues or {}
|
self.mockReturnValues = returnValues or {}
|
||||||
self.mockExpectations = {}
|
self.mockExpectations = {}
|
||||||
|
self.realClass = realClass
|
||||||
self.realClassMethods = None
|
self.realClassMethods = None
|
||||||
if realClass:
|
if realClass:
|
||||||
self.realClassMethods = dict(inspect.getmembers(realClass, inspect.isroutine))
|
self.realClassMethods = dict(inspect.getmembers(realClass, inspect.isroutine))
|
||||||
|
@ -92,7 +94,7 @@ class Mock:
|
||||||
self._setupSubclassMethodInterceptors()
|
self._setupSubclassMethodInterceptors()
|
||||||
|
|
||||||
def _setupSubclassMethodInterceptors(self):
|
def _setupSubclassMethodInterceptors(self):
|
||||||
methods = inspect.getmembers(self.__class__,inspect.isroutine)
|
methods = inspect.getmembers(self.realClass,inspect.isroutine)
|
||||||
baseMethods = dict(inspect.getmembers(Mock, inspect.ismethod))
|
baseMethods = dict(inspect.getmembers(Mock, inspect.ismethod))
|
||||||
for m in methods:
|
for m in methods:
|
||||||
name = m[0]
|
name = m[0]
|
||||||
|
@ -119,7 +121,7 @@ class Mock:
|
||||||
if self.realClassMethods is None:
|
if self.realClassMethods is None:
|
||||||
return
|
return
|
||||||
if name not in self.realClassMethods:
|
if name not in self.realClassMethods:
|
||||||
raise MockInterfaceError("Calling mock method '%s' that was not found in the original class" % name)
|
return
|
||||||
|
|
||||||
func = self.realClassMethods[name]
|
func = self.realClassMethods[name]
|
||||||
try:
|
try:
|
||||||
|
@ -268,7 +270,7 @@ class MockCallable:
|
||||||
def makeCall(self, params, kwparams):
|
def makeCall(self, params, kwparams):
|
||||||
if self.handcrafted:
|
if self.handcrafted:
|
||||||
allPosParams = (self.mock,) + params
|
allPosParams = (self.mock,) + params
|
||||||
func = _findFunc(self.mock.__class__, self.name)
|
func = _findFunc(self.mock.realClass, self.name)
|
||||||
if not func:
|
if not func:
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
return func(*allPosParams, **kwparams)
|
return func(*allPosParams, **kwparams)
|
||||||
|
|
Loading…
Reference in New Issue