编程语言
首页 > 编程语言> > php – Symfony Form测试成员函数create()on null

php – Symfony Form测试成员函数create()on null

作者:互联网

我正试图测试我的表格.我读到了:

但我得到一个null异常

class MediaTypeTest extends TypeTestCase
{
    protected function setUp()
    {

    }

    protected function tearDown()
    {
    }

    // tests
    public function testMe()
    {

        $formData = array(
            'test' => 'test',
            'test2' => 'test2',
        );

        $form = $this->factory->create(MediaType::class);

        // submit the data to the form directly
        $form->submit($formData);

        $this->assertTrue($form->isSynchronized());
        $this->assertEquals(new Media(), $form->getData());

        $view = $form->createView();
        $children = $view->children;

        foreach (array_keys($formData) as $key) {
            $this->assertArrayHasKey($key, $children);
        }
    }
}

我知道线路越野车是:

$form = $this->factory->create(MediaType::class);

但我该如何解决?

我发布:

phpunit tests/unit/Form/MediaTypeTest.php

或者通过代码:

php vendor/bin/codecept run unit Form/MediaTypeTest.php

任何的想法 ?

解决方法:

工厂对象是initializated in the setup method of the parent test class,因此您应该在testCase的setup方法中调用parent(或删除它的空实现).

所以一般记得在覆盖inherit方法时调用父类方法:

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

protected function tearDown()
{
    parent::tearDown();
}

希望这有帮助

标签:php,unit-testing,phpunit,symfony,codeception
来源: https://codeday.me/bug/20190627/1309107.html