2016-11-28 75 views
4

我試圖測試我的表單。我看書上說:Symfony Form測試成員函數create null()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); 
     } 
    } 
} 

據我所知,線車是:

$形成= $這個 - >出廠>創建(::的MediaType類);

但我該如何解決?

我推出:

PHPUnit測試/單位/表格/ MediaTypeTest.php

或通過codeception:

PHP供應商/斌/ codecept運行單元表/ MediaTypeTest .php

有什麼想法嗎?

+0

檢查要擴展權類:'的Symfony \分量\表格\測試\ Type \ TestCase代替'Symfony \ Component \ Form \ Tests \ Extension \ Validator \ Type \ TypeTestCase'。讓我知道 – Matteo

回答

2

工廠對象是initializated in the setup method of the parent test class所以你應該叫parent在你的測試用例setup方法(或取出空實現它)

所以一般記得調用父類的方法,當你重寫繼承方法:

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

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

希望這有助於

相關問題