【发布时间】:2022-02-03 04:02:10
【问题描述】:
这是我想要测试的观点:
# views.py
from weather.utils import get_weather_data
class CityWeather(APIView):
def get(self, request):
city = request.GET.get('city', None)
if city:
city_weather = get_weather_data(city)
if city_weather:
return Response(city_weather, status=status.HTTP_200_OK)
return Response({"error": "City was not found"}, status=status.HTTP_404_NOT_FOUND)
到目前为止,这是我的tests.py:
class TestWeatherAPI(TestCase):
def test_get_weather_data(self):
with mock.patch('weather.utils.get_weather_data') as mock_get:
response_data = {
"current_temperature": "3.1°C",
"current_pressure": 1033,
"current_humidity": 86
}
mock_get.return_value = response_data
response = self.client.get(reverse('get_city_weather'), {'city': 'London'})
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json(), response_data)
如我们所见,我只想修补视图中的get_weather_data。我该怎么做?
【问题讨论】:
-
我编辑了您的问题以添加该信息。请仔细检查我是否正确。
标签: django django-rest-framework python-unittest