Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/299.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 如何在django rest框架中模拟定制节流类进行测试?_Python_Django_Unit Testing_Testing_Django Rest Framework - Fatal编程技术网

Python 如何在django rest框架中模拟定制节流类进行测试?

Python 如何在django rest框架中模拟定制节流类进行测试?,python,django,unit-testing,testing,django-rest-framework,Python,Django,Unit Testing,Testing,Django Rest Framework,我在api.throttle.py print(“在自定义类之前”) 等级CustomThrottle(基本油门): 定义初始化(自): super()。\uuuu init\uuuuu() 打印(“初始化自定义油门”,自) self.\u wait=0 def允许_请求(自我、请求、查看): 打印(“CustomThrottle.allow_请求”) 如果安全方法中的request.method: 返回真值 #这里有一些支票吗 如果等待>0: self.\u wait=等待 返回错误 返回真值

我在
api.throttle.py

print(“在自定义类之前”)
等级CustomThrottle(基本油门):
定义初始化(自):
super()。\uuuu init\uuuuu()
打印(“初始化自定义油门”,自)
self.\u wait=0
def允许_请求(自我、请求、查看):
打印(“CustomThrottle.allow_请求”)
如果安全方法中的request.method:
返回真值
#这里有一些支票吗
如果等待>0:
self.\u wait=等待
返回错误
返回真值
def等待(自我):
返回自我。请稍候
我的
api.views.py
类似于:

来自api.throttle导入自定义throttle
打印(“视图模块”)
类SomeView(APIView):
打印(“视图中”)
节流阀\u类=[CustomThrottle]
def post(自我、请求、应存在):
#一些加工
返回响应({“message”:“Done.”)
我的测试是
api/tests/test\u views.py

@patch.object(api.views.CustomThrottle,“允许请求”)
def测试\u可\u获得注册用户的\u确认\u代码\u(自动、油门):
throttle.return\u值=真
response=self.client.post(路径,数据=data)
自我评价资格(
response.status_代码,
status.HTTP_200_OK,
“应该成功”,
)
@补丁(“api.views.CustomThrottle”)
def测试_读入(自、油门):
throttle.return\u value.allow\u request.return\u value=True
response=self.client.post(路径,数据=data)
自我评价资格(
response.status_代码,
status.HTTP_200_OK,
“应该成功”,
)


第一个测试正确通过,但仍然实例化了
CustomThrottle
类,但模拟了
allow\u request
方法;第二个测试表明
CustomThrottle
类和
allow_request
方法都没有被模拟,并且它在
状态429
下失败(
CustomThrottle
的节流速率为2分钟)。

如果您还想模拟
CustomThrottle
类及其属性,您还需要在需要的地方使用
patch
,因为
patch
decorator在您调用它的地方应用
monkey patching

关于你的情况,你可以做如下事情

from unittest.mock import MagicMock, patch

    @patch('api.views.CustomThrottle')
    @patch('api.views.PhoneConfirmationThrottle')
    def test_learn(self, phone_throttle, custom_throttle):
        mocked_custom_throttle_instance = MagicMock()
        mocked_custom_throttle_instance.allow_request = True
        mocked_custom_throttle_instance.status = ...

        # Do some stuff which do you need with the mocked class
        # and then return an instance for your patched object

        custom_throttle.return_value = mocked_custom_throttle_instance

        response = self.client.post(path, data=data)
        self.assertEqual(
            response.status_code,
            status.HTTP_200_OK,
            "Should be successful",
        )

它将用模拟的对象替换你的
真实的
对象,也可以检查一下,它将在将来对你有所帮助。

在花了一些时间测试不同的场景后(在这里和那里放置类似调试的打印语句:))我终于找到了它不起作用的原因,但它可能没有我想的那么正确,所以欢迎提供更好的答案


我定义了节流类列表,比如
throttle\u classes=[CustomThrottle]
因此,当服务器启动时,它会被导入和评估,因此在我的
SomeView
类中有一个对
CustomThrottle
的实际和未经修改版本的引用,并且在处理响应时,它会将其用于实例化,因此我的测试失败,但当我像
patch.object(CustomThrottle,“allow\u request”)一样对其进行修补时
当视图实际需要检查节流时,它将调用类似于
CustomThrottle().allow_request(…)
(注意对象创建时的括号);对象本身没有类似于
allow\u request
的方法,因此它在其类中搜索该方法并正确使用模拟版本

你想在第二个测试中模拟,
CustomThrottle
,对吗?@myuz,是的,在第一个测试中,
allow\u request
方法被模拟,但在第二个测试中,我想模拟整个
CustomThrottle
类。很抱歉,有一个打字错误,我使用了类似于
@patch('api.views.CustomThrottle')的补丁
但它并没有模仿我的视图使用的类。
mocked\u custom\u throttle\u实例
来自哪里?您在测试中定义,
mocked\u custom\u throttle\u实例=MagicMock()
,然后为修补对象返回它
custom\u throttle.return\u value=mocked\u custom\u throttle\u实例
,这对你有帮助吗?我忘了说我用过这种方式,贴了一个答案;我建议你复习一下。