示例#1
0
 def testReturnValueForFollowingCallsIsCached(self):
   result = object()
   mock_fn = mock.Mock(side_effect=[result])
   mock_fn.__name__ = compatibility.NativeStr("MockFunction")
   fn = utils.RunOnce(mock_fn)
   self.assertIs(fn(), result)
   self.assertIs(fn(), result)
示例#2
0
 def testDecoratedFunctionIsCalledAtLeastOnce(self):
   mock_fn = mock.Mock()
   mock_fn.__name__ = compatibility.NativeStr("MockFunction")
   fn = utils.RunOnce(mock_fn)
   mock_fn.assert_not_called()
   fn()
   mock_fn.assert_called_once()
示例#3
0
 def testExceptionsArePassedThrough(self):
   mock_fn = mock.Mock(side_effect=ValueError())
   mock_fn.__name__ = compatibility.NativeStr("MockFunction")
   fn = utils.RunOnce(mock_fn)
   with self.assertRaises(ValueError):
     fn()
   with self.assertRaises(ValueError):
     fn()
示例#4
0
 def testDecoratedFunctionIsCalledAtMostOnce(self):
   mock_fn = mock.Mock(side_effect=[None, AssertionError()])
   mock_fn.__name__ = compatibility.NativeStr("MockFunction")
   fn = utils.RunOnce(mock_fn)
   fn()
   fn()
   fn()
   mock_fn.assert_called_once()
示例#5
0
 def testWrapsFunctionProperly(self):
   mock_fn = mock.Mock()
   mock_fn.__name__ = compatibility.NativeStr("MockFunction")
   fn = utils.RunOnce(mock_fn)
   self.assertEqual(fn.__name__, compatibility.NativeStr("MockFunction"))
示例#6
0
 def testReturnValueIsPassedThrough(self):
   mock_fn = mock.Mock(return_value="bar")
   mock_fn.__name__ = compatibility.NativeStr("MockFunction")
   fn = utils.RunOnce(mock_fn)
   self.assertEqual("bar", fn())
示例#7
0
 def testArgumentsArePassedThrough(self):
   mock_fn = mock.Mock()
   mock_fn.__name__ = compatibility.NativeStr("MockFunction")
   fn = utils.RunOnce(mock_fn)
   fn(1, 2, foo="bar")
   mock_fn.assert_called_once_with(1, 2, foo="bar")