Reputation: 4173
I'm trying to test my form. I read that :
But I get a null
exception
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);
}
}
}
I understand that the line buggy is :
$form = $this->factory->create(MediaType::class);
But how can I resolve ?
I launch :
phpunit tests/unit/Form/MediaTypeTest.php
Or via codeception :
php vendor/bin/codecept run unit Form/MediaTypeTest.php
Any idea ?
Upvotes: 4
Views: 617
Reputation: 39470
The factory object is initializated in the setup method of the parent test class so You should call the parent
in the setup
method of your testCase (or remove the empty implementation of it).
So in general remember to call the parent class method when you override the inherit method:
protected function setUp()
{
parent::setUp();
}
protected function tearDown()
{
parent::tearDown();
}
Hope this help
Upvotes: 3