1

表单测试返回错误

Tests\AppBundle\Form\Type\UserPreferencesTypeTest::testUserPreferencesForm Symfony\Component\OptionsResolver\Exception\UndefinedOptionsException:选项“约束”不存在。定义的选项有:“action”、“attr”、“auto_initialize”、“block_name”、“by_reference”、“choice_attr”、“choice_label”、“choice_loader”、“choice_name”、“choice_translation_domain”、“choice_value”、“choices” ”、“choices_as_values”、“compound”、“data”、“data_class”、“disabled”、“empty_data”、“error_bubbling”、“expanded”、“group_by”、“inherit_data”、“label”、“label_attr”、 “标签格式”,“

这是我的表格

public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('global_review_count_threshold', ChoiceType::class, array(
            'choices'  => array(
                '5%' => 5,
                '10%' => 10,
                '15%' => 15,
                '20%' => 20,
                '25%' => 25,
                '30%' => 30,
                '35%' => 35,
                '40%' => 40,
                '45%' => 45,
                '50%' => 50,
            ), 'constraints' => array( new NotBlank(),), 'label' => "Global Review Count Threshold",
            'data' => $options['review_count_choice']
        ));
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'review_count_choice' => null,
        ));
    }

    public function getName()
    {
        return 'app_bundle_user_preferences_form';
    }

和我的表格测试

public function testUserPreferencesForm()
    {
        $formData = array(
            'global_review_count_threshold' => '10%',
        );

        $form = $this->factory->create(UserPreferencesType::class);

        //$object = TestObject::fromArray($formData);

        $form->submit($formData);

        $this->assertTrue($form->isSynchronized());
        //$this->assertEquals($object, $form->getData());

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

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

1 回答 1

0

我也在寻找这个问题的解决方案。

现在我建议使用 TestObject 设置器。例子:

    $formData = array(
        'email' => 'test@gmail.com',
        'surname' => 'Doe',
    );

    $form = $this->factory->create(RecipientType::class);

    $object = new Recipient();
    $object->setEmail($formData['email']);
    $object->setSurname($formData['surname']);

这允许我通过断言

$this->assertEquals($object, $form->getData());

也许这不是最好的解决方案,但可以正常工作

于 2017-09-12T10:34:17.550 回答