2

我有一个简单的事件处理程序来查找实际更改的内容(它已为IObjectModifiedEvent事件注册),代码如下所示:

def on_change_do_something(obj, event):
    modified = False
    # check if the publication has changed
    for change in event.descriptions:
        if change.interface == IPublication:
            modified = True
            break

    if modified:
        # do something

所以我的问题是:如何以编程方式生成这些描述?我到处都在使用 plone.app.dexterity,所以 z3c.form 在使用表单时会自动执行此操作,但我想用单元测试对其进行测试。

4

2 回答 2

4

event.description 名义上是一个 IModificationDescription 对象,它本质上是一个 IAttributes 对象列表:每个 Attributes 对象都有一个接口(例如模式)和属性(例如字段名称列表)被修改。

最简单的解决方案是为每个更改的字段创建一个 zope.lifecycleevent.Attributes 对象,并作为参数传递给事件构造函数——例如:

# imports elided...

changelog = [
    Attributes(IFoo, 'some_fieldname_here'),
    Attributes(IMyBehaviorHere, 'some_behavior_provided_fieldname_here',
    ]
notify(ObjectModifiedEvent(context, *changelog)
于 2016-02-18T18:02:05.167 回答
0

我也可能误解了一些东西,但你可以简单地在你的代码中触发事件,使用相同的参数,如 z3c.form (类似于@keul 的评论)?

在 Plone 4.3.x 中进行简短搜索后,我在以下位置找到了这个z3c.form.form

def applyChanges(self, data):
    content = self.getContent()
    changes = applyChanges(self, content, data)
    # ``changes`` is a dictionary; if empty, there were no changes
    if changes:
        # Construct change-descriptions for the object-modified event
        descriptions = []
        for interface, names in changes.items():
            descriptions.append(
                zope.lifecycleevent.Attributes(interface, *names))
        # Send out a detailed object-modified event
        zope.event.notify(
            zope.lifecycleevent.ObjectModifiedEvent(content, *descriptions))
    return changes

你需要两个测试用例,一个什么都不做,一个通过你的代码。

applyChanges位于同一个模块 (z3c.form.form) 中,它遍历表单字段并计算包含所有更改的 dict。

你应该在那里设置一个断点来检查字典是如何构建的。

之后,您可以在测试用例中执行相同的操作。

这样您就可以编写可读的测试用例。

def test_do_something_in_event(self)

    content = self.get_my_content()
    descriptions = self.get_event_descriptions()

    zope.event.notify(zope.lifecycleevent.ObjectModifiedEvent(content, *descriptions))

    self.assertSomething(...)        

恕我直言,嘲笑整个逻辑可能对未来来说是个坏主意,如果代码发生变化并且可能工作方式完全不同,那么您的测试仍然可以。

于 2016-02-18T16:13:18.603 回答