2012-07-02 21 views
0

我已經定義了一個簡單的Django-TastyPie API(它是派生自ModelResource的基本類;我使用0.9.11,最新穩定版)。 UserProfileListResource類獲取用JSON格式化的用戶列表(因此,內容類型爲application/json)。我沒有複製代碼,因爲它在手動測試時工作正常。使用Django測試客戶端測試TastyPie API總是會產生空的HTTP 200響應

我有它在urls.py註冊如下:

v1_api = Api(api_name='v1') 
v1_api.register(UserProfileListResource()) 

urlpatterns += patterns('', 
    (r'^api/', include(v1_api.urls))) 

當我測試使用的瀏覽器手動它工作正常。

但是,當我編寫使用django.test.client.Client發送請求的簡單測試用例時,我收到一個帶有空響應內容和錯誤內容類型(text/html而不是應用程序)的HTTP 200響應/ JSON)。

我在我的API代碼中引入了一些日誌語句,並且我意識到我的API代碼根本沒有被執行。

從我所看到的情況來看,還有其他成功的人。有什麼明顯的我失蹤?我卡住了! :-(

請在下面找到我的測試代碼,我的測試類從django.utils.unittest.TestCase繼承(我也曾嘗試django.test.testcases.TestCase;但沒有喜悅):

admin_user_profile = self._create_user_profile() 

browser_client = Client() 
response = browser_client.get(self.LIST_USERS_URL) 
self.assertTrue(HttpResponse.status_code, response.status_code) 

logging.debug(response._headers) #prints {'content-type': ('Content-Type', 'text/html; charset=utf-8'), 'location': ('Location', 'http://testserver/api/v1/users/?username=user&api_key=1681f96d50ba54b410c91365a678ec07b6375efc')} 
logging.debug(response.content) #prints empty string 

回答

2

我弄清楚我做錯了什麼,我從django.test.testcases.TestCase繼承,而不是django.test.TestCase(unittests.TestCase不起作用),當我改變它時,一切都開始了工作

我希望這可以幫助別人與同一個問題作鬥爭