plone.app.testing无法调用BrowserView

Ale*_*kov 5 python testing plone

我通过paster添加了几个BrowserViews,现在我正在尝试从plone.app.testing运行它们,因为我喜欢可重复且一致的测试.从浏览器手动调用视图可以正常工作.

我已尝试手动导入和初始化视图,以及从受限制的遍历调用该类.在这两种情况下,对象初始化都很好但是当我尝试触发渲染要么将实例作为函数调用,要么使用__call __()方法时,我得到以下错误:

*** KeyError: 'global_cache_settings
Run Code Online (Sandbox Code Playgroud)

我在plone.app.test中查看README.rst中的示例,它似乎没有提到问题,快速谷歌搜索也没有产生任何结果.它可能是网站定制之一,但是在greping源代码时,我发现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.
Run Code Online (Sandbox Code Playgroud)

这是相关的类声明:

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}
Run Code Online (Sandbox Code Playgroud)

模板:

<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>
Run Code Online (Sandbox Code Playgroud)

configure.zcml声明:

<browser:page
    for="*"
    name="promoboardhome"
    class=".promoboardhome.PromoBoardHome"
    allowed_interface=".promoboardhome.IPromoBoardHome"
    permission="zope2.View"
/>
Run Code Online (Sandbox Code Playgroud)

测试文件:

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()
Run Code Online (Sandbox Code Playgroud)

我在这里做错了吗?

hve*_*rde 5

浏览器视图只是命名的多适配器; 测试它们你已经在正确的上下文中调用它们并手动设置请求以提供为视图声明的接口; 在你的情况下可能是这样的:

from zope.interface import alsoProvides
from Products.MyProduct.browser.promoboardhome import IPromoBoardHome

class TestPromoBoardHome(unittest.TestCase):

    layer = PROD_INTEGRATION_TESTING

    def setUp(self):
        self.portal = self.layer['portal']
        self.request = self.layer['request']
        alsoProvides(self.request, IPromoBoardHome)
Run Code Online (Sandbox Code Playgroud)

我在像collective.nitf这样的包上有一堆浏览器视图测试.

快乐的测试!