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

时间:2015-12-14 10:45:56

标签: symfony testing symfony-forms phpspec

我在为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());
}

这是我的表单类:

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'
        ));
}

1 个答案:

答案 0 :(得分:3)

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

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

不应该......?

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