测试 Symfony2 表单的选项值时出现 PhpSpec 错误

PhpSpec error when testing Symfony2 form for choices values

我在为我的 Symfony 表单编写 PhpSpec 时遇到问题。这是我 运行 规范时 PhpSpec 吐出的错误。

当我在表单和规范中注释掉 displayMode 字段时,规范工作正常。

  46  ! builds form with file field
        method call:
          - add("displayMode", "choice", ["label" => "Display Mode", "choices" => ["st_andrews" => "St Andrews", "ayr_belleisle" => "Ayr Belleisle"]])
        on Double\Symfony\Component\Form\FormBuilder\P1 was not expected, expected calls were:
          - getFormFactory()
          - addEventSubscriber(type(Sylius\Bundle\TaxonomyBundle\Form\EventListener\BuildTaxonFormSubscriber))
          - add(exact("translations"), exact("a2lix_translationsForms"), *)
          - add(exact("file"), exact("crmpicco_media_file"), exact(["label" => "Course Image"]))
          - add(exact("displayMode"), exact("choice"), exact(["label" => "Course Image", "choices" => ["st_andrews" => "St Andrews", "ayr_belleisle" => "Ayr Belleisle"]]))
          - remove(exact("file"))

----  broken examples

这是我的规格:

function it_builds_form(FormBuilder $builder, FormFactoryInterface $factory)
{
    $builder->getFormFactory()->willReturn($factory);

    $builder
        ->addEventSubscriber(
            Argument::type('Sylius\Bundle\TaxonomyBundle\Form\EventListener\BuildTaxonFormSubscriber')
        )
        ->willReturn($builder);

    $builder
        ->add('translations', 'a2lix_translationsForms', Argument::any())
        ->willReturn($builder);


    $builder->add('file', 'crmpicco_media_file', array('label' => 'Course Image'))->shouldBeCalled();
    $builder->add('displayMode', 'choice',
        array(
            'label' => 'Display Mode',
            'choices' => Course::getAvailableDisplayModes()
        ))->shouldBeCalled();
    $builder->remove('file')->shouldBeCalled();

    $this->buildForm($builder, array());
}

这是我的表格class:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    parent::buildForm($builder, $options);

    // remove the standard image upload
    $builder->remove('file');

    // ...then add the custom asset file/image upload
    $builder
        ->add('displayMode', 'choice', array(
            'label'   => 'Display Mode',
            'choices' => Course::getAvailableDisplayModes()
        ))
        ->add('file', 'crmpicco_media_file', array(
            'label' => 'Course Image'
        ));
}

您可能已经发现了它,但在您的规格示例中:

$builder->add('displayMode', 'choice', [
    'label' => 'Course Image',
     //...
])->shouldBeCalled();

这不应该是...吗?

$builder->add('displayMode', 'choice', [
    'label' => 'Display Mode',
     //...
])->shouldBeCalled();