如何在单元测试中使用webapp2获取uri_for?
我正在尝试使用 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
我缺少一些愚蠢的设置吗?
I'm trying to unit test a handler with webapp2 and am running into what has to be just a stupid little error.
I'd like to be able to use webapp2.uri_for in the test, but I can't seem to do that:
def test_returns_200_on_home_page(self):
response = main.app.get_response(webapp2.uri_for('index'))
self.assertEqual(200, response.status_int)
If I just do main.app.get_response('/')
it works just fine.
The exception received is:
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
Is there some silly setup I'm missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为唯一的选择是设置一个虚拟请求,以便能够为测试创建 URI:
切勿在测试之外使用
set_globals()
。由 WSGI 应用程序调用,以线程安全的方式设置活动应用程序和请求。I think the only option is to set a dummy request just to be able to create URIs for the test:
Never use
set_globals()
outside of tests. Is is called by the WSGI application to set the active app and request in a thread-safe manner.webapp2.uri_for()
假设您位于 Web 请求上下文中,但由于找不到request
对象而失败。您可以将您的应用程序视为一个黑匣子,并使用文字 URI 来调用它,而不是解决这个问题,例如您提到的
'/'
。毕竟,您想要模拟正常的 Web 请求,并且 Web 浏览器也将使用 URI 而不是内部路由快捷方式。webapp2.uri_for()
assumes that you are in a web request context and it fails because it cannot find therequest
object.Instead of working around this you could think of your application as a black box and call it using literal URIs, like
'/'
as you mention it. After all, you want to simulate a normal web request, and a web browser will also use URIs and not internal routing shortcuts.