Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何模拟/修补应用程序引擎';他是一个装修工吗?_Python_Unit Testing_Google App Engine_Oauth_Mocking - Fatal编程技术网

Python 如何模拟/修补应用程序引擎';他是一个装修工吗?

Python 如何模拟/修补应用程序引擎';他是一个装修工吗?,python,unit-testing,google-app-engine,oauth,mocking,Python,Unit Testing,Google App Engine,Oauth,Mocking,AppEngine的python客户端库具有以下装饰器 @decorator.oauth_required from auth import decorator class ListUsersHandler(webapp2.RequestHandler): @decorator.oauth_required def get(self): self.response.write(_RenderUserListTemplate()) 但是,为单元测试模拟/修补实际上并不简单。

AppEngine的python客户端库具有以下装饰器

@decorator.oauth_required
from auth import decorator

class ListUsersHandler(webapp2.RequestHandler):

  @decorator.oauth_required
  def get(self):
    self.response.write(_RenderUserListTemplate())
但是,为单元测试模拟/修补实际上并不简单。例如,在下面的get处理程序中,我需要去掉oauth装饰器

@decorator.oauth_required
from auth import decorator

class ListUsersHandler(webapp2.RequestHandler):

  @decorator.oauth_required
  def get(self):
    self.response.write(_RenderUserListTemplate())
我试过下面的方法

from mock import patch
patch('decorator.oauth_required', lambda x: x).start()
import user

class MyTest(unittest.TestCase):
  def setUp(self):
    app = webapp2.WSGIApplication([('/', user.ListUsersHandler)])
    self.testapp = webtest.TestApp(app)

  def testListUsersHandler(self):
    response = self.testapp.get('/')
    self.assertTrue(('list tokens' in response))
但是,我看到的这个错误,似乎并没有给出多少线索

Traceback (most recent call last):
  File "user_test.py", line 44, in testAbc
    response = self.testapp.get('/')
  File "/usr/local/lib/python2.7/dist-packages/webtest/app.py", line 322, in get
    expect_errors=expect_errors)
  File "/usr/local/lib/python2.7/dist-packages/webtest/app.py", line 605, in do_request
    res = req.get_response(app, catch_exc_info=True)
  File "/google_appengine/lib/webob-1.2.3/webob/request.py", line 1292, in send
application, catch_exc_info=True)
  File "/google_appengine/lib/webob-1.2.3/webob/request.py", line 1269, in call_application
    return (captured[0], captured[1], app_iter, captured[2])
IndexError: list index out of range

您需要提供一个模仿
@decorator.oauth_required
功能的函数,而不仅仅是
lambda x:x
。如果您从处理程序中删除了decorator并运行了测试,它会工作吗?@JoshTriiJohnston:是的,如果删除了decorator,测试肯定会工作。我确实尝试过“剔除”装饰器,但不确定如何使用python mock实现。我下面的补丁不工作('decorator.oauth_required',lambda x:x).start()