Symfony2使用试图测试它的实体的自定义表单类型 [英] Symfony2 Custom form type using entity trying to test it

查看:222
本文介绍了Symfony2使用试图测试它的实体的自定义表单类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图测试一个表单类型,我创建了一个使用类实体的字段



这里是创建表单

  $ builder 
- > add('name','text')
- > add('description',' textarea')
- > add('services','entity',array('class'=>'MyBundle:Service','group_by'=>'category.name','property'= >'name','multiple'=> true,'required'=> false));

当我构建表单时,这种方式非常好,但是我试图单元测试这种类型



按照这个例子 a>关于如何测试我的自定义表单类型



我得到这个错误



Symfony \Component\\ \\ Form \Exception\Exception:无法加载类型实体



该错误是在单元测试开始时在此命令处导致的:

  $ type = new MyType(); 
$ form = $ this-> factory-> create($ type);

关于如何解决这个错误以便使用实体测试我的自定义表单类型的想法?提前感谢

解决方案

我猜你不能单元测试表单与实体类型,因为它被定义为服务。您是否尝试过添加它手动



编辑:恕我直言,你应该嘲笑实体类型,因为它涉及教义,它依赖于现有的数据库连接等完整的内核加载。所以你不再是单元测试。这将是一个功能测试。也许这就是为什么它不适用于单元测试的原因。


I am trying to test a form type I have creating that uses a field with class entity

here is the creation of the form

$builder
            ->add('name', 'text')
            ->add('description', 'textarea')
            ->add('services', 'entity', array('class' => 'MyBundle:Service', 'group_by' => 'category.name', 'property' => 'name', 'multiple' => true, 'required' => false));

This works very nice when I build the form, but then I am trying to unit test this type

Following this example on how to test my custom form types

I am getting this error

Symfony\Component\Form\Exception\Exception: Could not load type "entity"

The error is caused at the beginning of unit test at this command:

    $type = new MyType();
    $form = $this->factory->create($type);

any ideas on how to fix this error in order to test my custom form type using entities?

thanks in advance

解决方案

I guess you can't unit test form with entity types, because it's defined as a service. Have you tried adding it manually?

EDIT: IMHO you should mock the entity type, because it involves doctrine, which depends on an existing database connection and so on the full kernel loaded. So you're not unit testing any more. This would be a functional test. Maybe this the reason, why it's not available in the unit test.

这篇关于Symfony2使用试图测试它的实体的自定义表单类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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