@pytest.mark.django_db
def my_test(
self,
client,
):
with monkeypatch_requests(resp_json={'values': []}):
r: JsonResponse = client.post(
self.endpoint,
self.request_payload,
content_type='application/json',
)
assert r.status_code == 201, r.content
assert r.json() == {'key': value}
PyCharm 正在抱怨
Unresolved attribute reference 'json' for class 'JsonResponse'
,但我可以使用它。
当我在调试器中运行测试并将鼠标悬停在
json
上时,我可以看到 functools.partial(<bound method ClientMixin._parse_json of <django.test.client.Client object at 0x7f3b81399d30>>, <JsonResponse status_code=201, "application/json">)
json方法由django测试客户端添加到响应对象中。
https://docs.djangoproject.com/en/4.2/topics/testing/tools/#django.test.Response.json
https://github.com/django/django/blob/80a5667c50d12b27a031a9ce0408a5b81bc4e684/django/test/client.py#L950
这不是
django.http.JsonResponse
的方法,这就是 PyCharm 抱怨的原因。