summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsmiddlek <smiddlek@b1010a0a-674b-0410-b734-77272b80c875>2010-06-17 19:45:51 +0000
committersmiddlek <smiddlek@b1010a0a-674b-0410-b734-77272b80c875>2010-06-17 19:45:51 +0000
commitec82e83379b172f4794b85b4fc40a2be43493f9e (patch)
tree43f2c21751c5aba0c9ed8cb53d4842ee81482bb1
parent45c697579174ee064c750fe53f0ab2947e984fe4 (diff)
downloadmox-ec82e83379b172f4794b85b4fc40a2be43493f9e.tar.gz
Allow methods that are stubbed out on class instances to have their
signatures verified. Initial patch submitted by vmalloc. Slight modificants to tests by steve.middlekauff. git-svn-id: http://pymox.googlecode.com/svn/trunk@50 b1010a0a-674b-0410-b734-77272b80c875
-rwxr-xr-xmox.py6
-rwxr-xr-xmox_test.py33
-rwxr-xr-xmox_test_helper.py4
3 files changed, 38 insertions, 5 deletions
diff --git a/mox.py b/mox.py
index 0cba96a..db6b771 100755
--- a/mox.py
+++ b/mox.py
@@ -230,7 +230,9 @@ class Mox(object):
# A list of types that should be stubbed out with MockObjects (as
# opposed to MockAnythings).
_USE_MOCK_OBJECT = [types.ClassType, types.FunctionType, types.InstanceType,
- types.ModuleType, types.ObjectType, types.TypeType]
+ types.ModuleType, types.ObjectType, types.TypeType,
+ types.MethodType, types.UnboundMethodType,
+ ]
# A list of types that may be stubbed out with a MockObjectFactory.
_USE_MOCK_FACTORY = [types.ClassType, types.ObjectType, types.TypeType]
@@ -754,7 +756,7 @@ class MockObject(MockAnything, object):
# If we are mocking a Function, then use the function, and not the
# __call__ method
method = None
- if type(self._class_to_mock) == types.FunctionType:
+ if type(self._class_to_mock) in (types.FunctionType, types.MethodType):
method = self._class_to_mock;
else:
method = getattr(self._class_to_mock, '__call__')
diff --git a/mox_test.py b/mox_test.py
index 58142f6..2e429e6 100755
--- a/mox_test.py
+++ b/mox_test.py
@@ -1478,11 +1478,14 @@ class MoxTest(unittest.TestCase):
self.mox.VerifyAll()
def testStubOutMethod(self):
- """Test that a method is replaced with a MockAnything."""
+ """Test that a method is replaced with a MockObject."""
test_obj = TestClass()
+ method_type = type(test_obj.OtherValidCall)
# Replace OtherValidCall with a mock.
self.mox.StubOutWithMock(test_obj, 'OtherValidCall')
- self.assert_(isinstance(test_obj.OtherValidCall, mox.MockAnything))
+ self.assertTrue(isinstance(test_obj.OtherValidCall, mox.MockObject))
+ self.assertFalse(type(test_obj.OtherValidCall) is method_type)
+
test_obj.OtherValidCall().AndReturn('foo')
self.mox.ReplayAll()
@@ -1491,7 +1494,7 @@ class MoxTest(unittest.TestCase):
self.mox.VerifyAll()
self.mox.UnsetStubs()
self.assertEquals('foo', actual)
- self.failIf(isinstance(test_obj.OtherValidCall, mox.MockAnything))
+ self.assertTrue(type(test_obj.OtherValidCall) is method_type)
def testStubOutClass_OldStyle(self):
"""Test a mocked class whose __init__ returns a Mock."""
@@ -1602,6 +1605,30 @@ class MoxTest(unittest.TestCase):
self.assertRaises(AttributeError, mox_test_helper.MyTestFunction, 1)
self.mox.UnsetStubs()
+ def _testMethodSignatureVerification(self, stubClass):
+ # If stubClass is true, the test is run against an a stubbed out class,
+ # else the test is run against a stubbed out instance.
+ if stubClass:
+ self.mox.StubOutWithMock(mox_test_helper.ExampleClass, "TestMethod")
+ obj = mox_test_helper.ExampleClass()
+ else:
+ obj = mox_test_helper.ExampleClass()
+ self.mox.StubOutWithMock(mox_test_helper.ExampleClass, "TestMethod")
+ self.assertRaises(AttributeError, obj.TestMethod)
+ self.assertRaises(AttributeError, obj.TestMethod, 1)
+ self.assertRaises(AttributeError, obj.TestMethod, nine=2)
+ obj.TestMethod(1, 2)
+ obj.TestMethod(1, 2, 3)
+ obj.TestMethod(1, 2, nine=3)
+ self.assertRaises(AttributeError, obj.TestMethod, 1, 2, 3, 4)
+ self.mox.UnsetStubs()
+
+ def testStubOutClassMethodVerifiesSignature(self):
+ self._testMethodSignatureVerification(stubClass=True)
+
+ def testStubOutObjectMethodVerifiesSignature(self):
+ self._testMethodSignatureVerification(stubClass=False)
+
def testStubOutObject(self):
"""Test than object is replaced with a Mock."""
diff --git a/mox_test_helper.py b/mox_test_helper.py
index 65ffafd..60f72aa 100755
--- a/mox_test_helper.py
+++ b/mox_test_helper.py
@@ -109,3 +109,7 @@ class CallableClass(object):
def MyTestFunction(one, two, nine=None):
pass
+
+class ExampleClass(object):
+ def TestMethod(self, one, two, nine=None):
+ pass