如何测试在模拟中调用该方法?

时间:2019-10-16 18:57:15

标签: python-3.x python-unittest

我有一个看起来像这样的课:

from operators import Operation
class A():
    def process(self, message):
        Operation(message).execute()

我有这个测试:

class TestA(unittest.TestCase):

   @mock.patch("path.to.A.Operation")
   def test_create_assertion_processor(self, mock_operation):
      message = {}
      mock_operation_instance = mock.MagicMock
      mock_operation.return_value = mock.MagicMock(return_value = mock_operation_instance)

      A().process(message)

      mock_operation.assert_called_once_with(message)
      mock_operation_instance.execute.assert_called()

最后一行不起作用。我得到:

AttributeError: type object 'MagicMock' has no attribute 'execute'

我在这里做什么错了?

1 个答案:

答案 0 :(得分:1)

我认为这只是一个错字,实例化您的第一个test

时缺少括号
MagicMock