为什么工厂在测试表格中为空?

时间:2017-05-24 15:26:42

标签: php forms symfony unit-testing phpunit

我正在尝试测试扩展TypeTestCase

的表单
class ProjectTypeTest extends TypeTestCase
{

    private $entityManager;
    private $securityContext;
    private $translator;
    private $formFactory;

    protected function setUp()
    {
        // mock any dependencies
        $this->entityManager = $this->createMock("Doctrine\ORM\EntityManagerInterface");
        $this->securityContext = $this->createMock("Symfony\Component\Security\Core\SecurityContextInterface");
        $this->translator = $this->createMock("Symfony\Component\Translation\TranslatorInterface");
        $this->formFactory = $this->createMock("AppBundle\FormTemplate\Factory\FormFactory");

    }    

    public function testSubmitValidData()
    {

        $type = new ProjectType($this->entityManager,$this->securityContext, $this->translator, $this->formFactory);


        $this->factory->create($type);

    }

}

但是,当我拨打$this->factory->create($type)时,返回:

  

在null

上调用成员函数create()

factory属性为空!

我正在使用Symfony\Component\Form\Test\TypeTestCase并且我还使用了use Symfony\Component\Form\Tests\Extension\Core\Type\TypeTestCase并返回相同的结果。

或者,我做得怎么样? 或者,我如何测试表格?

1 个答案:

答案 0 :(得分:1)

问题是$this->factory未初始化。这应该由FormIntegrationTestCase完成,TypeTestCase扩展,然后由您的测试类扩展。

您的setUp()覆盖原始setUp(),初始化$this->factory,因此您应该致电父母{/ p>}

protected function setUp()
{
    parent::setUp();

    // mock any dependencies
    $this->entityManager = $this->createMock("Doctrine\ORM\EntityManagerInterface");
    $this->securityContext = $this->createMock("Symfony\Component\Security\Core\SecurityContextInterface");
    $this->translator = $this->createMock("Symfony\Component\Translation\TranslatorInterface");
    $this->formFactory = $this->createMock("AppBundle\FormTemplate\Factory\FormFactory");

}