也许我错过了一些东西……哦,我想是的,但找不到答案。
WebTestCase
生成此构造函数示例:
public function __construct(?string $name = null, array $data = [], string $dataName = '')
{
parent::__construct($name, $data, $dataName);
}
试图将我的服务添加为第一个或最后一个参数 - Symfony 抛出错误:
Type error: Too few arguments to function Tests\AppBundle\Manager\ContactManagerTest::__construct(), 0 passed in /Library/WebServer/Documents/HEPT/vendor/bin/.phpunit/phpunit-5.7/src/Framework/TestSuite.php on line 568 and at least 1 expected in /Library/WebServer/Documents/HEPT/tests/AppBundle/Manager/ContactManagerTest.php:22
我应该以某种方式直接使用容器吗?为什么自动装配不起作用WebTestCase
课程是否有桥梁课程?
WebTestCase 在 PHPUnit 的上下文中使用(与 Symfony 及其依赖注入无关)。
他们实际上生成了内核及其容器,请参阅提取自的这段代码Symfony 源代码 https://github.com/symfony/symfony/blob/7a8b7060841eaa2494f6b64777759a7d38895d2e/src/Symfony/Bundle/FrameworkBundle/Test/WebTestCase.php#L35:
protected static function createClient(array $options = array(), array $server = array())
{
$kernel = static::bootKernel($options);
$client = $kernel->getContainer()->get('test.client');
$client->setServerParameters($server);
return $client;
}
这意味着您可以像这样轻松访问容器:
$kernel = static::bootKernel($options);
$container = $kernel->getContainer();
另请注意static::$kernel->getContainer()
在您创建客户端进行测试后即可使用。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)