2012-05-27 28 views
3

我有返回類似的觀點:如何關注測試中的金字塔重定向?

headers = remember(request, str(user.id)) 
return HTTPFound(location=request.route_url('home'), headers=headers) 

,我正在寫一個測試,但我如何遵循上面的代碼重定向?我仍然得到HTTPFound對象,並且它的response.request應該是發起響應的請求給了我None。

這裏是我的測試代碼如下至今:

request = testing.DummyRequest(
    post=MultiDict(email='[email protected]', password='random')) 
response = login(request) 

這裏,響應是HTTPFound但我如何跟隨重定向家?

回答

4

我意識到,這是不使用DummyRequest

我會推薦功能測試,因爲WebTest使得它更容易做,和管理。

在返回重定向的響應上,您可以撥打follow來遵循完整的請求。

http://webtest.pythonpaste.org/en/latest/index.html

redirect_response = self.testapp.post(
    '/signup', params=post_params, status=302) 
full_response = redirect_response.follow() 
+0

是啊,我想這將是最接近的事情。但關於這一點的是,響應現在是一個HTML字符串嗎?我的意思是,如果視圖返回一個帶有一些對象的字典,我想檢查它存儲哪個響應屬性? – Marconi

+0

如果呈現的響應+視圖返回的實際字典是非常好的。 – Marconi