[英]Django unittest function with redirect based on mock return_value
I have a view function similar to我有一个类似于 function 的视图
def my_function(request):
session = create_something('some_random_string')
return redirect(session.url, code=303)
To test it测试一下
import unittest
from django.test import TestCase
from unittest.mock import patch
from my_app.views import my_function
class TestMyFunction(TestCase):
@patch('my_app.views.create_something', return_value={
"url": "https://tiagoperes.eu/"
})
def test_my_function(self, mock_create_something):
response = self.client.get("/my-function/")
This gives这给
AttributeError: 'dict' object has no attribute 'url'
AttributeError: 'dict' object 没有属性 'url'
This question is similar the following questions这个问题类似于以下问题
redirect()
that takes dynamic values coming from the mocked function, so can also reach easily the error TypeError: quote_from_bytes() expected bytes
)redirect()
,它采用来自模拟 function 的动态值,因此也可以轻松达到错误TypeError: quote_from_bytes() expected bytes
)return_value
I'd get TypeError: expected string or bytes-like object
)return_value
,我会得到TypeError: expected string or bytes-like object
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.