diff options
Diffstat (limited to 'mox_test.py')
-rwxr-xr-x | mox_test.py | 179 |
1 files changed, 146 insertions, 33 deletions
diff --git a/mox_test.py b/mox_test.py index f865fb9..d489f0b 100755 --- a/mox_test.py +++ b/mox_test.py @@ -33,7 +33,7 @@ class ExpectedMethodCallsErrorTest(unittest.TestCase): self.assertRaises(ValueError, mox.ExpectedMethodCallsError, []) def testOneError(self): - method = mox.MockMethod("testMethod", [], False) + method = mox.MockMethod("testMethod", [], [], False) method(1, 2).AndReturn('output') e = mox.ExpectedMethodCallsError([method]) self.assertEqual( @@ -42,13 +42,13 @@ class ExpectedMethodCallsErrorTest(unittest.TestCase): str(e)) def testManyErrors(self): - method1 = mox.MockMethod("testMethod", [], False) + method1 = mox.MockMethod("testMethod", [], [], False) method1(1, 2).AndReturn('output') - method2 = mox.MockMethod("testMethod", [], False) + method2 = mox.MockMethod("testMethod", [], [], False) method2(a=1, b=2, c="only named") - method3 = mox.MockMethod("testMethod2", [], False) + method3 = mox.MockMethod("testMethod2", [], [], False) method3().AndReturn(44) - method4 = mox.MockMethod("testMethod", [], False) + method4 = mox.MockMethod("testMethod", [], [], False) method4(1, 2).AndReturn('output') e = mox.ExpectedMethodCallsError([method1, method2, method3, method4]) self.assertEqual( @@ -457,8 +457,9 @@ class MockMethodTest(unittest.TestCase): """Test class to verify that the MockMethod class is working correctly.""" def setUp(self): - self.expected_method = mox.MockMethod("testMethod", [], False)(['original']) - self.mock_method = mox.MockMethod("testMethod", [self.expected_method], + self.expected_method = mox.MockMethod("testMethod", [], [], False)( + ['original']) + self.mock_method = mox.MockMethod("testMethod", [self.expected_method], [], True) def testNameAttribute(self): @@ -523,18 +524,18 @@ class MockMethodTest(unittest.TestCase): def testEqualityNoParamsEqual(self): """Methods with the same name and without params should be equal.""" - expected_method = mox.MockMethod("testMethod", [], False) + expected_method = mox.MockMethod("testMethod", [], [], False) self.assertEqual(self.mock_method, expected_method) def testEqualityNoParamsNotEqual(self): """Methods with different names and without params should not be equal.""" - expected_method = mox.MockMethod("otherMethod", [], False) + expected_method = mox.MockMethod("otherMethod", [], [], False) self.failIfEqual(self.mock_method, expected_method) def testEqualityParamsEqual(self): """Methods with the same name and parameters should be equal.""" params = [1, 2, 3] - expected_method = mox.MockMethod("testMethod", [], False) + expected_method = mox.MockMethod("testMethod", [], [], False) expected_method._params = params self.mock_method._params = params @@ -542,7 +543,7 @@ class MockMethodTest(unittest.TestCase): def testEqualityParamsNotEqual(self): """Methods with the same name and different params should not be equal.""" - expected_method = mox.MockMethod("testMethod", [], False) + expected_method = mox.MockMethod("testMethod", [], [], False) expected_method._params = [1, 2, 3] self.mock_method._params = ['a', 'b', 'c'] @@ -551,7 +552,7 @@ class MockMethodTest(unittest.TestCase): def testEqualityNamedParamsEqual(self): """Methods with the same name and same named params should be equal.""" named_params = {"input1": "test", "input2": "params"} - expected_method = mox.MockMethod("testMethod", [], False) + expected_method = mox.MockMethod("testMethod", [], [], False) expected_method._named_params = named_params self.mock_method._named_params = named_params @@ -559,7 +560,7 @@ class MockMethodTest(unittest.TestCase): def testEqualityNamedParamsNotEqual(self): """Methods with the same name and diffnamed params should not be equal.""" - expected_method = mox.MockMethod("testMethod", [], False) + expected_method = mox.MockMethod("testMethod", [], [], False) expected_method._named_params = {"input1": "test", "input2": "params"} self.mock_method._named_params = {"input1": "test2", "input2": "params2"} @@ -575,39 +576,39 @@ class MockMethodTest(unittest.TestCase): instB = TestClass(); params = [instA, ] - expected_method = mox.MockMethod("testMethod", [], False) + expected_method = mox.MockMethod("testMethod", [], [], False) expected_method._params = params self.mock_method._params = [instB, ] self.assertEqual(self.mock_method, expected_method) def testStrConversion(self): - method = mox.MockMethod("f", [], False) + method = mox.MockMethod("f", [], [], False) method(1, 2, "st", n1=8, n2="st2") self.assertEqual(str(method), ("f(1, 2, 'st', n1=8, n2='st2') -> None")) - method = mox.MockMethod("testMethod", [], False) + method = mox.MockMethod("testMethod", [], [], False) method(1, 2, "only positional") self.assertEqual(str(method), "testMethod(1, 2, 'only positional') -> None") - method = mox.MockMethod("testMethod", [], False) + method = mox.MockMethod("testMethod", [], [], False) method(a=1, b=2, c="only named") self.assertEqual(str(method), "testMethod(a=1, b=2, c='only named') -> None") - method = mox.MockMethod("testMethod", [], False) + method = mox.MockMethod("testMethod", [], [], False) method() self.assertEqual(str(method), "testMethod() -> None") - method = mox.MockMethod("testMethod", [], False) + method = mox.MockMethod("testMethod", [], [], False) method(x="only 1 parameter") self.assertEqual(str(method), "testMethod(x='only 1 parameter') -> None") - method = mox.MockMethod("testMethod", [], False) + method = mox.MockMethod("testMethod", [], [], False) method().AndReturn('return_value') self.assertEqual(str(method), "testMethod() -> 'return_value'") - method = mox.MockMethod("testMethod", [], False) + method = mox.MockMethod("testMethod", [], [], False) method().AndReturn(('a', {1: 2})) self.assertEqual(str(method), "testMethod() -> ('a', {1: 2})") @@ -746,7 +747,7 @@ class MethodCheckerTest(unittest.TestCase): """Tests MockMethod's use of MethodChecker method.""" def testNoParameters(self): - method = mox.MockMethod('NoParameters', [], False, + method = mox.MockMethod('NoParameters', [], [], False, CheckCallTestClass.NoParameters) method() self.assertRaises(AttributeError, method, 1) @@ -755,7 +756,7 @@ class MethodCheckerTest(unittest.TestCase): self.assertRaises(AttributeError, method, 1, b=2) def testOneParameter(self): - method = mox.MockMethod('OneParameter', [], False, + method = mox.MockMethod('OneParameter', [], [], False, CheckCallTestClass.OneParameter) self.assertRaises(AttributeError, method) method(1) @@ -766,7 +767,7 @@ class MethodCheckerTest(unittest.TestCase): self.assertRaises(AttributeError, method, 1, b=2) def testTwoParameters(self): - method = mox.MockMethod('TwoParameters', [], False, + method = mox.MockMethod('TwoParameters', [], [], False, CheckCallTestClass.TwoParameters) self.assertRaises(AttributeError, method) self.assertRaises(AttributeError, method, 1) @@ -783,7 +784,7 @@ class MethodCheckerTest(unittest.TestCase): self.assertRaises(AttributeError, method, 3, a=1, b=2) def testOneDefaultValue(self): - method = mox.MockMethod('OneDefaultValue', [], False, + method = mox.MockMethod('OneDefaultValue', [], [], False, CheckCallTestClass.OneDefaultValue) method() method(1) @@ -794,7 +795,7 @@ class MethodCheckerTest(unittest.TestCase): self.assertRaises(AttributeError, method, 1, b=2) def testTwoDefaultValues(self): - method = mox.MockMethod('TwoDefaultValues', [], False, + method = mox.MockMethod('TwoDefaultValues', [], [], False, CheckCallTestClass.TwoDefaultValues) self.assertRaises(AttributeError, method) self.assertRaises(AttributeError, method, c=3) @@ -814,7 +815,7 @@ class MethodCheckerTest(unittest.TestCase): self.assertRaises(AttributeError, method, a=1, b=2, e=9) def testArgs(self): - method = mox.MockMethod('Args', [], False, CheckCallTestClass.Args) + method = mox.MockMethod('Args', [], [], False, CheckCallTestClass.Args) self.assertRaises(AttributeError, method) self.assertRaises(AttributeError, method, 1) method(1, 2) @@ -825,7 +826,7 @@ class MethodCheckerTest(unittest.TestCase): self.assertRaises(AttributeError, method, 1, 2, c=3) def testKwargs(self): - method = mox.MockMethod('Kwargs', [], False, CheckCallTestClass.Kwargs) + method = mox.MockMethod('Kwargs', [], [], False, CheckCallTestClass.Kwargs) self.assertRaises(AttributeError, method) method(1) method(1, 2) @@ -840,7 +841,7 @@ class MethodCheckerTest(unittest.TestCase): self.assertRaises(AttributeError, method, 1, 2, 3, 4) def testArgsAndKwargs(self): - method = mox.MockMethod('ArgsAndKwargs', [], False, + method = mox.MockMethod('ArgsAndKwargs', [], [], False, CheckCallTestClass.ArgsAndKwargs) self.assertRaises(AttributeError, method) method(1) @@ -1031,7 +1032,7 @@ class MockObjectTest(unittest.TestCase): self.assertRaises(mox.UnexpectedMethodCallError, call) - dummy._Verify() + self.assertRaises(mox.SwallowedExceptionError, dummy._Verify) def testMockSetItem_WithSubClassOfNewStyleClass(self): class NewStyleTestClass(object): @@ -1099,7 +1100,7 @@ class MockObjectTest(unittest.TestCase): self.assertRaises(mox.UnexpectedMethodCallError, call) - dummy._Verify() + self.assertRaises(mox.SwallowedExceptionError, dummy._Verify) def testMockGetItem_WithSubClassOfNewStyleClass(self): class NewStyleTestClass(object): @@ -1169,7 +1170,7 @@ class MockObjectTest(unittest.TestCase): self.assertRaises(mox.UnexpectedMethodCallError, call) - dummy._Verify() + self.assertRaises(mox.SwallowedExceptionError, dummy._Verify) def testMockIter_ExpectedIter_NoSuccess(self): """Test that __iter__() gets mocked in Dummy. @@ -1711,7 +1712,7 @@ class MoxTest(unittest.TestCase): self.assertRaises(mox.UnexpectedMethodCallError, TestClass.OtherValidCall, "wrong self") - self.mox.VerifyAll() + self.assertRaises(mox.SwallowedExceptionError, self.mox.VerifyAll) self.mox.UnsetStubs() def testStubOutMethod_Unbound_NamedUsingPositional(self): @@ -2001,6 +2002,118 @@ class MoxTest(unittest.TestCase): self.assertEquals('MockMethod has no attribute "ShowMeTheMoney". ' 'Did you remember to put your mocks in replay mode?', str(e)) + def testSwallowedUnknownMethodCall(self): + """Test that a swallowed UnknownMethodCallError will be re-raised.""" + dummy = self.mox.CreateMock(TestClass) + dummy._Replay() + + def call(): + try: + dummy.InvalidCall() + except mox.UnknownMethodCallError: + pass + + # UnknownMethodCallError swallowed + call() + + self.assertRaises(mox.SwallowedExceptionError, self.mox.VerifyAll) + + def testSwallowedUnexpectedMockCreation(self): + """Test that a swallowed UnexpectedMockCreationError will be re-raised.""" + self.mox.StubOutClassWithMocks(mox_test_helper, 'CallableClass') + self.mox.ReplayAll() + + def call(): + try: + mox_test_helper.CallableClass(1, 2) + except mox.UnexpectedMockCreationError: + pass + + # UnexpectedMockCreationError swallowed + call() + + self.assertRaises(mox.SwallowedExceptionError, self.mox.VerifyAll) + self.mox.UnsetStubs() + + def testSwallowedUnexpectedMethodCall_WrongMethod(self): + """Test that a swallowed UnexpectedMethodCallError will be re-raised. + + This case is an extraneous method call.""" + mock_obj = self.mox.CreateMockAnything() + mock_obj.Open() + self.mox.ReplayAll() + + def call(): + mock_obj.Open() + try: + mock_obj.Close() + except mox.UnexpectedMethodCallError: + pass + + # UnexpectedMethodCall swallowed + call() + + self.assertRaises(mox.SwallowedExceptionError, self.mox.VerifyAll) + + def testSwallowedUnexpectedMethodCall_WrongArguments(self): + """Test that a swallowed UnexpectedMethodCallError will be re-raised. + + This case is an extraneous method call.""" + mock_obj = self.mox.CreateMockAnything() + mock_obj.Open() + self.mox.ReplayAll() + + def call(): + try: + mock_obj.Open(1) + except mox.UnexpectedMethodCallError: + pass + + # UnexpectedMethodCall swallowed + call() + + self.assertRaises(mox.SwallowedExceptionError, self.mox.VerifyAll) + + def testSwallowedUnexpectedMethodCall_UnorderedGroup(self): + """Test that a swallowed UnexpectedMethodCallError will be re-raised. + + This case is an extraneous method call in an unordered group.""" + mock_obj = self.mox.CreateMockAnything() + mock_obj.Open().InAnyOrder() + mock_obj.Close().InAnyOrder() + self.mox.ReplayAll() + + def call(): + mock_obj.Close() + try: + mock_obj.Open(1) + except mox.UnexpectedMethodCallError: + pass + + # UnexpectedMethodCall swallowed + call() + + self.assertRaises(mox.SwallowedExceptionError, self.mox.VerifyAll) + + def testSwallowedUnexpectedMethodCall_MultipleTimesGroup(self): + """Test that a swallowed UnexpectedMethodCallError will be re-raised. + + This case is an extraneous method call in a multiple times group.""" + mock_obj = self.mox.CreateMockAnything() + mock_obj.Open().MultipleTimes() + self.mox.ReplayAll() + + def call(): + try: + mock_obj.Open(1) + except mox.UnexpectedMethodCallError: + pass + + # UnexpectedMethodCall swallowed + call() + + self.assertRaises(mox.SwallowedExceptionError, self.mox.VerifyAll) + class ReplayTest(unittest.TestCase): """Verify Replay works properly.""" |