如何测试query_params?
如何测试query_params?我提供了一个答复和请求,但它仍然不起作用。最初,错误是这样的:
price = int(self.context.get('request').query_params.get('price', None))
AttributeError: 'NoneType' object has no attribute 'query_params'
test_api.py
class IpotekaApiTestCase(APITestCase):
def setUp(self) -> None:
self.offer_1 = Ipoteka.objects.create(bank_name='test',
term_min=1,
term_max=3,
rate_min=1.5,
rate_max=4.8,
payment_min=12345,
payment_max=123456)
self.offer_2 = Ipoteka.objects.create(bank_name='test_1',
term_min=1,
term_max=3,
rate_min=1.5,
rate_max=4.8,
payment_min=12345,
payment_max=123456)
def test_get(self):
url = reverse('offer-list')
response = self.client.get(url)
view = IpotekaModelViewSet.as_view({'get': 'list'})
request = RequestFactory().get('/')
context = {'request': view(request), 'response': response}
serializer_data = IpotekaSerializer([self.offer_1, self.offer_2],
context=context, many=True).data
self.assertEqual(status.HTTP_200_OK, response.status_code)
self.assertEqual(serializer_data, response.data['results'])
serializers.py
def get_payment(self, obj) -> int:
try:
price = int(self.context.get('request').query_params.get('price', None))
deposit = int(self.context.get('request').query_params.get('deposit',
None))
term = int(self.context.get('request').query_params.get('term', None))
if price == 0 or term == 0:
return 0
return self._get_offer_result(price, deposit, term, obj)
except (ValueError, TypeError):
return 0
出现了此错误:
File "D:\Work\Django\Test_ipoteka\ipoteka\serializers.py", line 22, in get_payment
price = int(self.context.get('request').query_params.get('price', None))
AttributeError: 'Response' object has no attribute 'query_params'
----------------------------------------------------------------------
Ran 1 test in 0.014s
FAILED (errors=1)
我还删除了响应,但没有帮助。
How can I test query_params? I have provided a response and a request, but it still doesn't work. Initially, the error was like this:
price = int(self.context.get('request').query_params.get('price', None))
AttributeError: 'NoneType' object has no attribute 'query_params'
test_api.py
class IpotekaApiTestCase(APITestCase):
def setUp(self) -> None:
self.offer_1 = Ipoteka.objects.create(bank_name='test',
term_min=1,
term_max=3,
rate_min=1.5,
rate_max=4.8,
payment_min=12345,
payment_max=123456)
self.offer_2 = Ipoteka.objects.create(bank_name='test_1',
term_min=1,
term_max=3,
rate_min=1.5,
rate_max=4.8,
payment_min=12345,
payment_max=123456)
def test_get(self):
url = reverse('offer-list')
response = self.client.get(url)
view = IpotekaModelViewSet.as_view({'get': 'list'})
request = RequestFactory().get('/')
context = {'request': view(request), 'response': response}
serializer_data = IpotekaSerializer([self.offer_1, self.offer_2],
context=context, many=True).data
self.assertEqual(status.HTTP_200_OK, response.status_code)
self.assertEqual(serializer_data, response.data['results'])
serializers.py
def get_payment(self, obj) -> int:
try:
price = int(self.context.get('request').query_params.get('price', None))
deposit = int(self.context.get('request').query_params.get('deposit',
None))
term = int(self.context.get('request').query_params.get('term', None))
if price == 0 or term == 0:
return 0
return self._get_offer_result(price, deposit, term, obj)
except (ValueError, TypeError):
return 0
Got this error:
File "D:\Work\Django\Test_ipoteka\ipoteka\serializers.py", line 22, in get_payment
price = int(self.context.get('request').query_params.get('price', None))
AttributeError: 'Response' object has no attribute 'query_params'
----------------------------------------------------------------------
Ran 1 test in 0.014s
FAILED (errors=1)
I also deleted response but it didn't help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您可以按以下内容设置查询参数。
I think you can set the query parameters like the following.
我在我的项目和我的每个应用程序中都在编写测试
。
案例 我在我的测试案例中以这种方式传递了
url = f'/api/v1/ducuntor_information/?employee_id = {self.employee.id}'
f'/api/api/v1/ducuntion_information/'这是我通过的基本URL和剩余的部分是查询parms
i m writing test case of my project and in my every apps there is using queryparms so basically query parms is use in my views is
employee_id = self.request.query_params.get('employee_id', None)
and i pass this in this way in my test case
url = f'/api/v1/education_information/?employee_id={self.employee.id}'
f'/api/v1/education_information/' this is the base url and remaining part is query parms that i passed