我有一個視圖的代碼服務文件下載,它在瀏覽器中正常工作。現在我正在嘗試使用內部django Client.get編寫測試:django測試文件下載 - 「ValueError:關閉的文件上的I/O操作」
response = self.client.get("/compile-book/", {'id': book.id})
self.assertEqual(response.status_code, 200)
self.assertEquals(response.get('Content-Disposition'),
"attachment; filename=book.zip")
迄今爲止這麼好。現在我想測試下載的文件是否是我期望它下載的文件。於是,我開始說:
f = cStringIO.StringIO(response.content)
現在我的測試運行與迴應:
Traceback (most recent call last):
File ".../tests.py", line 154, in test_download
f = cStringIO.StringIO(response.content)
File "/home/epub/projects/epub-env/lib/python2.7/site-packages/django/http/response.py", line 282, in content
self._consume_content()
File "/home/epub/projects/epub-env/lib/python2.7/site-packages/django/http/response.py", line 278, in _consume_content
self.content = b''.join(self.make_bytes(e) for e in self._container)
File "/home/epub/projects/epub-env/lib/python2.7/site-packages/django/http/response.py", line 278, in <genexpr>
self.content = b''.join(self.make_bytes(e) for e in self._container)
File "/usr/lib/python2.7/wsgiref/util.py", line 30, in next
data = self.filelike.read(self.blksize)
ValueError: I/O operation on closed file
即使我做的只是:self.assertIsNotNone(response.content)我得到同樣的ValueError異常
只有整個互聯網上的主題(包括django文檔)我可以找到關於測試下載的是這個計算器主題:Django Unit Test for testing a file download。嘗試該解決方案導致了這些結果。對於我來說,這已經很老了,很難再提出一個新問題。
任何人都知道如何測試下載應該在Django中處理? (順便說一句,在Python 2.7上運行Django 1.5)
你找到一個解決這個問題?我現在正在遇到一模一樣的問題。 – Alex