我一直在追逐实现模拟测试Pylons控制器功能的问题。
我想测试的特定功能可以在这里找到:https://hg.mozilla.org/build/buildapi/file/efe11511f42d/buildapi/controllers/selfserve.py#l210
目标:
我只是试图模拟它调用的函数,以便我可以确认它最终调用get_completeness。
问题:
我可以模拟_ok,_get_stable_delay,_failed和_branches_cache就好了。但是当涉及到g.buildapi_cache.get_builds_for_revision时,我似乎无法正确地模拟它,每次调用revision()时,都会失败。
g实际上是pylons.app_globals并在selfserve.py控制器中导入,如下所示:'from pylons import app_globals as g'
测试
我正在使用python解释器来测试所有这些。这些是我正在尝试的两个实现:http://pastebin.mozilla.org/2931825和http://pastebin.mozilla.org/2931832
总而言之,这是解释器中的完整运行,完成了http://pastebin.mozilla.org/2931825的跟踪
>>> from buildapi.controllers.selfserve import SelfserveController
>>> from mock import patch, Mock
>>> s = SelfserveController()
>>> s._ok = Mock(name='_ok')
>>> s._get_stable_delay = Mock(return_value=180)
>>> s._branches_cache = Mock()
>>> s._branches_cache = {'try': None}
>>> s._failed = Mock(return_value="Failed")
>>> with patch('buildapi.controllers.selfserve.g.buildapi_cache.get_builds_for_revision') as get_builds_for_revision:
... get_builds_for_revision.return_value = ['baz', 'bang']
... result = revision('try', 'bar')
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/jzeller/buildapi-test/lib/python2.7/site-packages/mock.py", line 1252, in __enter__
self.target = self.getter()
File "/Users/jzeller/buildapi-test/lib/python2.7/site-packages/mock.py", line 1414, in <lambda>
getter = lambda: _importer(target)
File "/Users/jzeller/buildapi-test/lib/python2.7/site-packages/mock.py", line 1102, in _importer
thing = _dot_lookup(thing, comp, import_path)
File "/Users/jzeller/buildapi-test/lib/python2.7/site-packages/mock.py", line 1089, in _dot_lookup
return getattr(thing, comp)
File "/Users/jzeller/buildapi-test/lib/python2.7/site-packages/paste/registry.py", line 137, in __getattr__
return getattr(self._current_obj(), attr)
File "/Users/jzeller/buildapi-test/lib/python2.7/site-packages/paste/registry.py", line 197, in _current_obj
'thread' % self.____name__)
TypeError: No object (name: app_globals) has been registered for this thread
这里是http://pastebin.mozilla.org/2931832
>>> from buildapi.controllers.selfserve import SelfserveController
>>> from mock import patch, Mock
>>> s = SelfserveController()
>>> s._ok = Mock(name='_ok')
>>> s._get_stable_delay = Mock(return_value=180)
>>> s._branches_cache = Mock()
>>> s._branches_cache = {'try': None}
>>> s._failed = Mock(return_value="Failed")
>>> g = Mock()
>>> g.buildapi_cache = Mock(name='buildapi_cache')
>>> g.buildapi_cache.get_builds_for_revision = Mock(name='get_builds_for_revision', return_value=['baz', 'bang'])
>>> s.revision('try', 'bar')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "buildapi/controllers/selfserve.py", line 217, in revision
job_items = g.buildapi_cache.get_builds_for_revision(branch, revision)
File "/Users/jzeller/buildapi-test/lib/python2.7/site-packages/paste/registry.py", line 137, in __getattr__
return getattr(self._current_obj(), attr)
File "/Users/jzeller/buildapi-test/lib/python2.7/site-packages/paste/registry.py", line 197, in _current_obj
'thread' % self.____name__)
TypeError: No object (name: app_globals) has been registered for this thread
这与我尝试运行修订时遇到的错误相同。每次在这里出错都是因为我试图模仿这个app_globals模块失败了,或者看起来如此。我真的不确定如何尝试这个。 有没有人有任何想法?
以下是一些更好的细节:
注1
我已经查看了以下线程,这些线程似乎并不适用于这种特定情况,或者我完全忽略了这一点。
答案 0 :(得分:0)
如果没有初始化的Pylons环境,控制器就不能用作独立的对象。
使用Pylons为测试提供的功能可以从所有样板代码中保存您以测试Controller。
我建议您将TestController类与nosetests结合使用,如documentation所示。
特别是对于您的代码,我认为Pylons将私有方法(以_开头)与 public 方法区别对待,因为公共方法在请求的上下文中运行。