模拟补丁检查父 class 方法调用
Mock Patch check parent class method call
我想对这个 class 方法进行单元测试 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
调用了什么,你必须调用原始版本,并且只模拟 functions/methods 在更新中调用。假设基础 class 像 import serializers
一样导入,你可以做这样的事情(未经测试)
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)
我想对这个 class 方法进行单元测试 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
调用了什么,你必须调用原始版本,并且只模拟 functions/methods 在更新中调用。假设基础 class 像 import serializers
一样导入,你可以做这样的事情(未经测试)
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)