代码之家  ›  专栏  ›  技术社区  ›  hvelarde

如何在测试中启用敏捷行为?

  •  1
  • hvelarde  · 技术社区  · 11 年前

    我为基于敏捷的内容类型编写了一个行为;它正在运行,但我不知道在测试中启用它的正确方法是什么。

    我使用了以下方法:

    def _enable_background_image_behavior(self):
        fti = queryUtility(IDexterityFTI, name='collective.cover.content')
        behaviors = list(fti.behaviors)
        behaviors.append(self.name)
        fti.behaviors = tuple(behaviors)
    
    def _disable_background_image_behavior(self):
        fti = queryUtility(IDexterityFTI, name='collective.cover.content')
        behaviors = list(fti.behaviors)
        behaviors.remove(self.name)
        fti.behaviors = tuple(behaviors)
    

    但该行为在某些Plone版本中似乎没有被禁用或启用(在Plone 4.2和Plone 4.3中表现不同,可能是因为敏捷度从1.x移动到2.x)。

    测试的完整代码如下: https://github.com/collective/collective.cover/blob/background-image-behavior/src/collective/cover/tests/test_behaviors.py

    Plone 4.2中的测试结果: https://travis-ci.org/collective/collective.cover/jobs/33327495

    在集成测试中启用和禁用行为的正确方法是什么?

    2 回复  |  直到 11 年前
        1
  •  2
  •   hvelarde    11 年前

    Asko,谢谢你为我指明了正确的方向:我最终以以下方式使模式缓存无效:

    from plone.dexterity.schema import SchemaInvalidatedEvent
    from zope.event import notify
    
    # invalidate schema cache
    notify(SchemaInvalidatedEvent('collective.cover.content'))
    
        2
  •  1
  •   Asko Soukka    11 年前

    我相信你做的是正确的,但问题与dx 1.x和2.x之间的缓存修复有关。我已经通过以下方法清除了测试设置中的dx缓存:

    def testSetUp(self):
        import plone.dexterity.schema
        for name in dir(plone.dexterity.schema.generated):
            if name.startswith("plone"):
                delattr(plone.dexterity.schema.generated, name)
        plone.dexterity.schema.SCHEMA_CACHE.clear()
    
    推荐文章