使用 unittest.mock 模拟 DRF 响应

Using unittest.mock to mock a DRF response

我的示例非常基础:

# urls.py
from django.urls import include, path
from rest_framework.routers import DefaultRouter
from core import views

router = DefaultRouter()
router.register(r'core', views.CoreViewSet)

urlpatterns = [
    path('', include(router.urls)),
]
# views.py
from rest_framework import mixins, viewsets
from .models import Numbers
from .serializers import NumbersSerializer


class CoreViewSet(viewsets.GenericViewSet,
              mixins.ListModelMixin):
    queryset = Numbers.objects.all()
    serializer_class = NumbersSerializer
# serializers.py
from rest_framework import serializers
from .models import Numbers

class NumbersSerializer(serializers.ModelSerializer):
    class Meta:
        model = Numbers
        fields = '__all__'
# models.py
from django.db import models

# Create your models here.
class Numbers(models.Model):
    odd = models.IntegerField()
    even = models.IntegerField()

    class Meta:
        db_table = 'numbers'

我想做的是模拟对 /core/ URI 的请求,因此它 returns 是一个模拟响应,而不是来自数据库的响应。例如,考虑在数据库不可用时在 CI 管道中进行单元测试。

下面是我所拥有的,但是 print(response.data) returns 实际响应而不是模拟响应:

import unittest
from unittest.mock import patch
from rest_framework.test import APIClient

class CoreTestCase(unittest.TestCase):
    
    @patch('core.views')
    def test_response(self, mock_get):
        mock_get.return_value = [{'hello': 'world'}]
        client = APIClient()
        response = client.get('/core/')
        print(response.data)

在解决这个问题时找不到非常直观的文档,所以问我应该如何实现它。建议?

@patch('core.views')

上面的补丁只是模拟文件,不会有任何效果。您可以使用的是:

@patch('core.views.CoreViewSet.list') 

这将模拟视图集的 list 方法,然后您可以将响应对象作为 return_value.