【问题标题】:PhpSpec error when testing Symfony2 form for choices values测试 Symfony2 表单的选择值时出现 PhpSpec 错误
【发布时间】:2015-12-14 10:45:56
【问题描述】:

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

【问题讨论】:

    标签: symfony testing symfony-forms phpspec


    【解决方案1】:

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

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

    这不应该是……吗?

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

    【讨论】:

    • 尽管正如@AdamElsodaney 所示,在技术上可以指定表单类,但它并没有多大价值。你会从为它编写集成测试中得到更多。 “不要嘲笑你不拥有的东西”。
    • @JakubZalas 您的意思是通过 behat 测试表单功能? “不要嘲笑你不拥有的东西”是什么意思?谢谢!
    • 我只需要编写一个 phpunit 测试来创建 from。如果您模拟第三方接口,您永远无法确定您的期望是否有效。谷歌“不要嘲笑你不拥有的东西”并阅读blog.8thlight.com/eric-smith/2011/10/27/thats-not-yours.html
    猜你喜欢
    • 2013-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-11
    • 2013-07-06
    • 2021-01-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多