【问题标题】:Symfony Form Testing member function create() on nullSymfony 表单测试成员函数 create() on null
【发布时间】:2017-04-12 09:51:06
【问题描述】:

我正在尝试测试我的表单。我读到了:

但我得到一个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 运行单元Form/MediaTypeTest.php

有什么想法吗?

【问题讨论】:

  • 检查您是否扩展了正确的类:Symfony\Component\Form\Test\TypeTestCase instead of Symfony\Component\Form\Tests\Extension\Validator\Type\TypeTestCase。告诉我

标签: php unit-testing symfony phpunit codeception


【解决方案1】:

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

所以一般记得在重写继承方法时调用父类方法:

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

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

希望有帮助

【讨论】:

    猜你喜欢
    • 2020-06-08
    • 1970-01-01
    • 1970-01-01
    • 2017-07-19
    • 2021-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-19
    相关资源
    最近更新 更多