简体   繁体   English

带有z3c.form的单元测试buttonAndHandler

[英]unit testing buttonAndHandler with a z3c.form

I am simply looking to write a unit test that tests the methods in my dexterity SchemaForm that use the buttonAndHandler, but I wasn't able to find anything appropriate in either the z3c.form documentation nor the Dexterity Developer Manual. 我只是想编写一个单元测试来测试我的敏捷性SchemaForm中使用buttonAndHandler的方法,但是在z3c.form文档或《敏捷开发人员手册》中都找不到任何合适的方法。 I believe I'm getting tripped up on the decorator behavior but I don't know how I should be programmatically calling these methods. 我相信我会被装饰器行为绊倒,但是我不知道应该如何以编程方式调用这些方法。

form = self.rf.restrictedTraverse('add-file')
#mform = getMultiAdapter((self.rf,self.request), name='add-file')

Using restrictedTraverse or getMultiAdapter yields the same object. 使用strictTraverse或getMultiAdapter会产生相同的对象。 So if I want to call form.addFileSendToEditors I pass the form as the first variable and what for "action"? 因此,如果我想调用form.addFileSendToEditors,则将表单作为第一个变量传递,那么“ action”又是什么呢?

Basically you could get the handlers from the form and call the manually. 基本上,您可以从表单获取处理程序并手动调用。

This is an example with a regular z3c.form and a Dexterity add form. 这是带有常规z3c.form和Dexterity添加表单的示例。

>>> form_view = self.rf.restrictedTraverse('add-file')

# If your form is a Dexterity DefaultAddForm view.
>>> form_view
<plone.dexterity.browser.add.DefaultAddView object at 0x10cbf0950>
# Get the form from the instance
>>> form_view.form
<class 'plone.dexterity.browser.add.DefaultAddForm'>


# Than you can get all handlers
>>> form_view.form.handlers
<Handlers [<Handler for <Button 'save' u'Save'>>, <Handler for <Button 'cancel' u'Cancel'>>]
# and all buttons
form_view.form.buttons.items()
[('save', <Button 'save' u'Save'>), ('cancel', <Button 'cancel' u'Cancel'>)]

# In _handlers you can see the buttons, with the corresponding handlers
form_view.form.handlers._handlers
((<Button 'save' u'Save'>, <Handler for <Button 'save' u'Save'>>), (<Button 'cancel' u'Cancel'>, Handler for <Button 'cancel' u'Cancel'>>))

# You can also get the handler by button
>>> save_button = form_view.form.buttons.items()[0]
>>> save_handler = form_view.form.handlers.getHandler(save_button)
<Handler for <Button 'save' u'Save'>>

# Once you have your handler, you can call it directly
save_handler.func(form_view.form_instance, save_button)

It depends on what you are doing if you have to setup a little bit more, to make your test work. 如果要进行更多设置才能使测试正常进行,则取决于您在做什么。 You did not give us enough informations about what you are doing in you handler. 您没有为我们提供有关处理程序中正在执行的操作的足够信息。

This is taken from the z3c.form documentation: I did not run this code for myself. 这摘自z3c.form文档:我没有亲自运行此代码。

# You can test your actions also this, probably more readable :-)
from z3c.form.testing import TestRequest
from z3c.form import button

>>> request = TestRequest(form={'form.buttons.save': 'Save'})
>>> actions = button.ButtonActions(form_view.form_instance, request, None)
>>> actions.update()
>>> actions.execute()
# This executes your Save actions. 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM