我想單元測試一個處理程序與webapp2和我跑到什麼必須只是一個愚蠢的小錯誤。如何在單元測試中使用webapp2獲得uri_for?
我希望能夠在測試中使用webapp2.uri_for,但我似乎無法做到這一點:
def test_returns_200_on_home_page(self):
response = main.app.get_response(webapp2.uri_for('index'))
self.assertEqual(200, response.status_int)
如果我只是做main.app.get_response('/')
它工作得很好。
收到的例外是:
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/case.py", line 318, in run
testMethod()
File "tests.py", line 27, in test_returns_200_on_home_page
webapp2.uri_for('index')
File "/Users/.../webapp2_example/lib/webapp2.py", line 1671, in uri_for
return request.app.router.build(request, _name, args, kwargs)
File "/Users/.../webapp2_example/lib/webapp2_extras/local.py", line 173, in __getattr__
return getattr(self._get_current_object(), name)
File "/Users/.../webapp2_example/lib/webapp2_extras/local.py", line 136, in _get_current_object
raise RuntimeError('no object bound to %s' % self.__name__)
RuntimeError: no object bound to request
有一些愚蠢的設置我失蹤?
感謝回覆@moraes。下班回家時我會試一試。順便說一下,我真的很喜歡webapp2,感謝與我們分享! – Aaron
這似乎不是很好。現在我收到:文件「/Users/.../webapp2_example/lib/webapp2.py」,行1671,在uri_for 返回request.app.router.build(request,_name,args,kwargs) AttributeError:'NoneType '對象沒有屬性'路由器' – Aaron
的確,並不那麼容易。我糾正了這個片段;希望現在可以運作。 – moraes