plone.app.testing无法调用BrowserView

时间:2012-11-11 04:18:19

标签: python testing plone

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

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

*** KeyError: 'global_cache_settings

我在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.

这是相关的类声明:

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()

我在这里做错了吗?

2 个答案:

答案 0 :(得分: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)

我对collective.nitf等软件包进行了一系列浏览器视图测试。

快乐测试!

答案 1 :(得分:2)

您可能希望使用testbrowser测试视图:

from plone.testing import z2
browser = z2.Browser(self.layer['app'])
browser.open(self.layer['portal'].absolute_url() + '/@@promoboardhome')

或类似。