5

我通过 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()

我在这里做错了吗?

4

2 回答 2

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这样的包进行了测试。

祝测试愉快!

于 2012-11-15T12:40:53.417 回答
2

您可能想用 testbrowser 测试视图:

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

或类似的。

于 2012-11-11T07:13:33.263 回答