0
我正在測試一個Django應用程序。當用戶登錄時,我將他重定向到圖形頁面。爲了測試,我寫Django重定向狀態代碼錯誤
response = TestCase.client.post('app/login', {credentials}, follow=True)
TestCase.assertRedirects(response, 'app/graphs')
返回
AssertionError: False is not true :
Response didn't redirect as expected: Response code was 200 (expected 302).
不夠合理。 The docs sayassertRedirects
有一個status_code
參數。然後調用
TestCase.assertRedirects(response, 'app/graphs', status_code=200)
回報
AssertionError: False is not true :
Response didn't redirect as expected: Response code was 200 (expected 200).
這是怎麼回事?
我在客戶端代碼中使用'window.location.href'來重定向,並希望確保我結束了正確的頁面。我怎麼能測試這個功能? –
您可以使用Django測試客戶端來檢查'response.content'是否包含'window.location.href'的正確值。但是,如果你想測試瀏覽器行爲,你應該使用類似Selenium的東西。請參閱[文檔](https://docs.djangoproject.com/en/1.9/topics/testing/tools/#the-test-client)以獲取更多解釋。 – Alasdair