Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 模拟补丁检查父类方法调用_Python_Unit Testing_Django Rest Framework_Mocking_Patch - Fatal编程技术网

Python 模拟补丁检查父类方法调用

Python 模拟补丁检查父类方法调用,python,unit-testing,django-rest-framework,mocking,patch,Python,Unit Testing,Django Rest Framework,Mocking,Patch,我想对这个类方法进行单元测试update class EmployeeUpdateSerializer(serializers.ModelSerializer): def update(self, instance, data): shift_types = data.pop('shift_types', None) instance = super().update(instance, data) self.update_shift_t

我想对这个类方法进行单元测试
update

class EmployeeUpdateSerializer(serializers.ModelSerializer):

    def update(self, instance, data):
        shift_types = data.pop('shift_types', None)
        instance = super().update(instance, data)
        self.update_shift_type(instance, shift_types)
        return instance
我正在这样做

class TestEmployeeUpdateSerializer(TestCase):
    def setUp(self):
        self.company, self.user, self.header = create_user_session()
        self.serializer = EmployeeUpdateSerializer()

    def test_update(self):
        employee = self.user

        with patch.object(self.serializer, 'update') as update:
            with patch.object(self.serializer, 'update_shift_type') as update_shift_type:
                res = self.serializer.update(employee, dict())

                update.assert_called_once_with(employee, dict())
                update_shift_type.assert_called_once_with(employee, None)

                self.assertEqual(res, employee)
但这给了我一个错误

    Traceback (most recent call last):
  File "/Users/shahzadfarukh/my-croft/backend/account/tests/test_employee_serializers.py", line 222, in test_update
    update_shift_type.assert_called_once_with(employee, None)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/unittest/mock.py", line 830, in assert_called_once_with
    raise AssertionError(msg)
AssertionError: Expected 'update_shift_type' to be called once. Called 0 times.

请帮帮我!我做错了什么吗?

您已经模拟了
更新,所以它的原始代码不会被调用。如果要测试
update
调用的内容,必须调用原始版本,并且只模拟更新中调用的函数/方法。假设基类是像导入序列化程序那样导入的,您可以这样做(未测试)


您已经模拟了
update
,因此不会调用它的原始代码。如果你想测试
update
调用的内容,你必须调用原始版本,并且只模拟
update
@mrbeanbreman中调用的函数/方法。你能帮我举个例子吗?
class TestEmployeeUpdateSerializer(TestCase):
    def setUp(self):
        self.company, self.user, self.header = create_user_session()
        self.serializer = EmployeeUpdateSerializer()

     @patch('serializers.ModelSerializer.update')
     @patch('serializers.ModelSerializer.update_shift_type')
     def test_update(self, mocked_update_shift_type, mocked_update):
         employee = self.user
         res = self.serializer.update(employee, dict())
         mocked_update.assert_called_once_with(employee, dict())
         mocked_update_shift_type.assert_called_once_with(employee, None)
         self.assertEqual(res, employee)