我正在尝试使用 webapp2 对处理程序进行单元测试,并且遇到了一个愚蠢的小错误。
我希望能够在测试中使用 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
我缺少一些愚蠢的设置吗?