gpt4 book ai didi

python - plone.app.testing 无法调用 BrowserView

转载 作者:太空狗 更新时间:2023-10-30 00:56:56 25 4
gpt4 key购买 nike

我已经通过 paster 添加了几个 BrowserViews,现在我正尝试从 plone.app.testing 运行它们,因为我喜欢可重复和一致的测试。从浏览器手动调用 View 没有任何问题。

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

*** KeyError: 'global_cache_settings

我在 plone.app.testing 中查看了 README.rst 中的示例,它似乎没有提到问题,快速谷歌搜索也没有产生任何结果。它可能是站点自定义之一,但是在 greping 源代码时,我发现 Products.CMFPlone 中提到了“global_cache_settings”,所以它可能是一个 plone 的东西:

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

我是不是做错了什么?

最佳答案

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

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 这样的包进行了一堆浏览器 View 测试.

测试愉快!

关于python - plone.app.testing 无法调用 BrowserView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13328534/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com