我一直在寻找实现模拟以测试 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 解释器来测试所有这些。这些是我正在尝试的 2 个实现:http: //pastebin.mozilla.org/2931825和http://pastebin.mozilla.org/2931832
总而言之,这里是解释器中的完整运行,带有http://pastebin.mozilla.org/2931825的 Traceback
>>> 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 模块失败了,或者看起来是这样。我真的不确定如何尝试这个。有没有人有任何想法?
这里有一些更好的细节:
- 这是我试图使用的函数,它在我正在实例化的类中:https ://hg.mozilla.org/build/buildapi/file/efe11511f42d/buildapi/controllers/selfserve.py#l210
注1
我已经查看了以下似乎不太适用于这个特定场景的线程,或者我完全错过了这一点。