我通过 paste 添加了几个 BrowserViews,现在我尝试从 plone.app.testing 运行它们,因为我喜欢可重复和一致的测试。从浏览器手动调用视图没有任何问题。
我尝试过手动导入和初始化视图,以及从受限遍历中调用类。在这两种情况下,对象都可以很好地初始化,但是当我尝试触发渲染时,将实例作为函数调用或使用 __call__() 方法,我收到以下错误:
*** KeyError: 'global_cache_settings
我查看了 plone.app.testing 中 README.rst 中的示例,它似乎没有提到问题,快速谷歌搜索也没有产生任何结果。它可能是站点自定义之一,但是在查找源代码时,我发现 Products.CMFPlone 中提到了“global_cache_settings”,所以这可能是一个克隆的事情:
eggs/Products.CMFPlone-4.1.4-py2.6.egg/Products/CMFPlone/skins/plone_templates/main_template.pt
28: <metal:cache use-macro="context/global_cache_settings/macros/cacheheaders">
29: Get the global cache headers located in global_cache_settings.
eggs/plone.app.layout-2.1.13-py2.6.egg/plone/app/layout/presentation/presentation.pt
11: <metal:cache use-macro="here/global_cache_settings/macros/cacheheaders">
12: Get the global cache headers located in global_cache_settings.
eggs/plonetheme.sunburst-1.1.6-py2.6.egg/plonetheme/sunburst/skins/sunburst_templates/main_template.pt
20: <metal:cache use-macro="context/global_cache_settings/macros/cacheheaders">
21: Get the global cache headers located in global_cache_settings.
这是相关的类声明:
from zope.interface import implements, Interface
from Products.Five import BrowserView
from Products.CMFCore.utils import getToolByName
from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile
from Products.MyProduct import MyProductMessageFactory as _
class IPromoBoardHome(Interface):
"""
PromoBoardHome view interface
"""
def __init__(self, context, request):
pass
def __call__(self):
pass
def test():
""" test method"""
class PromoBoardHome(BrowserView):
"""
PromoBoardHome browser view
"""
implements(IPromoBoardHome)
def __init__(self, context, request):
self.context = context
self.request = request
def __call__(self):
return ViewPageTemplateFile('pt/promoboardhome.pt')(self)
@property
def portal_catalog(self):
return getToolByName(self.context, 'portal_catalog')
@property
def portal(self):
return getToolByName(self.context, 'portal_url').getPortalObject()
def test(self):
"""
test method
"""
dummy = _(u'a dummy string')
return {'dummy': dummy}
模板:
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"
lang="en"
metal:use-macro="here/main_template/macros/master"
i18n:domain="Products.MyProduct">
<body>
<div metal:fill-slot="main">
<tal:main-macro metal:define-macro="main"
tal:define="testview view/test">
<span tal:content="testview/dummy">test</span>
</tal:main-macro>
</div>
</body>
</html>
configure.zcml 声明:
<browser:page
for="*"
name="promoboardhome"
class=".promoboardhome.PromoBoardHome"
allowed_interface=".promoboardhome.IPromoBoardHome"
permission="zope2.View"
/>
测试文件:
import unittest2 as unittest
from . import PROD_INTEGRATION_TESTING
from plone.app.testing import setRoles, TEST_USER_ID
from Products.CMFCore.utils import getToolByName
from Products.MyProduct.browser.promoboardhome import PromoBoardHome
class TestPromoBoardHome(unittest.TestCase):
layer = PROD_INTEGRATION_TESTING
def setUp(self):
self.portal = self.layer['portal']
def test_call(self):
pbh = self.portal.restrictedTraverse('@@promoboardhome')
try:
pbh()
except KeyError:
print "that didn't work!"
import pdb; pdb.set_trace()
我在这里做错了吗?