如何在单元测试中使用webapp2获得uri_for? [英] How to get uri_for with webapp2 in unit test?

查看:139
本文介绍了如何在单元测试中使用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('/')就可以了。

收到的例外是:

 追踪):
文件/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/case.py,行318,运行
testMethod()
文件tests.py,第27行,在test_returns_200_on_home_page
webapp2.uri_for('index')
文件/Users/.../webapp2_example/lib/webapp2.py,第1671行,in uri_for
返回request.app.router.build(request,_name,args,kwargs)
文件/Users/.../webapp2_example/lib/webapp2_extras/local.py,第173行,in __getattr__
getattr(self._get_current_object(),name)
文件/Users/.../webapp2_example/lib/webapp2_extras/local.py,第136行,位于_get_current_object
中RuntimeError('no object bound to%s'%self .__ name__)
RuntimeError:no object bound to request

我是否缺少一些愚蠢的设置?

解决方案

<我认为唯一的选择是设置一个虚拟请求,以便为测试创建URI: (self):
//设置一个虚拟请求只是为了能够使用uri_for()。
req = webapp2.Request.blank('/')
req.app = main.app
main.app.set_globals(app = main.app,request = req)

response = main.app.get_response(webapp2.uri_for('index'))
self.assertEqual(200,response.status_int)

切勿在测试之外使用 set_globals()。是由WSGI应用程序调用以线程安全方式设置活动应用程序和请求。


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?

解决方案

I think the only option is to set a dummy request just to be able to create URIs for the test:

def test_returns_200_on_home_page(self):
    // Set a dummy request just to be able to use uri_for().
    req = webapp2.Request.blank('/')
    req.app = main.app
    main.app.set_globals(app=main.app, request=req)

    response = main.app.get_response(webapp2.uri_for('index'))
    self.assertEqual(200, response.status_int)

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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆