![](/img/trans.png)
[英]Calling View in API Request Factory when testing in Django Rest Framework
[英]Django Rest API Test Request Factory Content Empty
在以下測試中,我得到了兩個錯誤之一,但我無法解決。 這是我在Django中的第一個項目,因此是第一次對其進行測試。 我讀到APIRequestFactory將為測試創建自己的數據庫,然后將其擦干凈。 與中間件沒有交互。
是否可以使用GET調用檢查數據是否已正確檢索? 到目前為止,我只能檢查狀態是否為200。 我正在執行的GET或POST調用的內容為空。 我在RequestFactory上看到的示例沒有顯示任何數據比較。 如果您知道任何示例,我應該可以解決我的問題。
self.assertEqual(response.data ['module'],report.module)TypeError:列表索引必須是整數,而不是str
我不確定為使此斷言正確而要進行哪些更改。 我已經搜索了錯誤消息,但是似乎我沒有使用ListField作為report.module,因為那是Report的直接字段。
AssertionError:[]!='[{“模塊”:“ testModule”}]'
class GetModules(TestCase):
def setUp(self):
#access to the request factory
self.factory = APIRequestFactory()
def test_get_modules(self):
self.assertEqual(Report.objects.count(), 0)
Report.objects.create(module="testModule", severity=3, title="Get Modules", data="{}")
report = Report.objects.get(module="testModule")
self.assertEqual(Report.objects.count(), 1)
url = '/dashboard/modules/'
request = self.factory.get(url)
response = moduleList(request)
self.assertEqual(Report.objects.count(), 1)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data, '[{"module":"testModule" }]')
我知道該對象存在,因為count的測試在創建對象之前和之后均返回0。
response.data已經是一個python對象,而不是字符串,因此我想它將等於{"module":"testModule" }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.