我正在尝试测试扩展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
并返回相同的结果。
或者,我做得怎么样? 或者,我如何测试表格?
答案 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");
}