有约束的Symfony2单元测试表格 [英] Symfony2 Unit Testing Forms with constraints

查看:68
本文介绍了有约束的Symfony2单元测试表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个未连接到实体的表单,但确实具有此处提到的约束:

I've got a form which isn't connected to a entity but does have constraints like mentioned in here: http://symfony.com/doc/current/book/forms.html#adding-validation

ContactBundle \ Tests \ Form \ Type \ TestedTypeTest :: testSubmitValidData Symfony \ Component \ OptionsResolver \ Exception \ InvalidOptionsException:选项约束"不存在.已知选项为:操作",属性",自动初始化",块名称","by_reference","compound","data","data_class","disabled","empty_data","error_bubbling","heritit_data" ," label," label_attr," mapped," max_length," method," pattern," post_max_size_message," property_path," read_only," required," translation_domain," trim, 虚拟"

ContactBundle\Tests\Form\Type\TestedTypeTest::testSubmitValidData Symfony\Component\OptionsResolver\Exception\InvalidOptionsException: The option "constraints" does not exist. Known options are: "action", "attr", "auto_initialize", "block_name", "by_reference", "compound", "data", "data_class", "disabled", "empty_data", "error_bubbling", "inherit_data", "label", "label_attr", "mapped", "max_length", "method", "pattern", "post_max_size_message", "property_path", "read_only", "required", "translation_domain", "trim", "virtual"

这是表格的一部分:

class ContactType extends AbstractType
{

/**
 * Build the form
 * @param \Symfony\Component\Form\FormBuilderInterface $builder BuilderInterface
 * @param array $aOption Array of options
 */
public function buildForm(FormBuilderInterface $builder, array $aOption)
{
    ///..
    $builder->add('name', 'text', array(
                'constraints' => array(
                    new NotBlank(),
                    new Length(array('min' => 3)),
                ),
            ))
            ->add('address', 'textarea', array(
                'required' => false
            ))
            //..
            ;
    //..

这是单元测试

class TestedTypeTest extends TypeTestCase
{

public function testSubmitValidData()
{
    $formData = array(
        'name' => 'Test Name',
        'address' => '',
    );
    $type = new ContactType();
    $form = $this->factory->create($type, $formData);
    $form->submit($formData);

    $this->assertTrue($form->isSynchronized());

    $view = $form->createView();
    $children = $view->children;

    foreach (array_keys($formData) as $key) {
        $this->assertArrayHasKey($key, $children);
    }
}
}

我猜这是测试的问题,而不是表单的问题,因为表单可以按预期工作.我不确定应该更改什么. 任何帮助或建议都将很方便

I'm guessing that this is a problem with the test instead of the form as the form works as expected. I'm not sure what I should change. Any help or advice would be handy

谢谢

推荐答案

我认为问题在于您还将formData传递给了工厂的create方法.

I think the problem is that you also pass the formData to the factory's create method.

就像代码中已有的那样,只需通过$form->submit($formData)传递表单数据就可以了

It should be fine to just pass the form data via $form->submit($formData) like you already have in your code

更多文档: http://symfony.com/doc/current/cookbook /form/unit_testing.html

似乎您必须在测试设置中将验证器/约束扩展添加到工厂对象中,如此处所述

Well it seems you have to add the validator/constaint extensions to the factory object in your test setup like stated here http://symfony.com/doc/current/cookbook/form/unit_testing.html#adding-custom-extensions

这篇关于有约束的Symfony2单元测试表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆