Car*_*sAS 5 php phpunit symfony
我正在尝试为使用全局参数(来自YML文件)的服务编写测试。
我正在方法中检索此参数setUp(),但是当我尝试在中使用它们时@dataProvider,会引发错误。
class InterpreterServiceTest extends KernelTestCase
{
private $container;
private $service;
private $citiesMap;
public function setUp()
{
self::bootKernel();
$this->container = self::$kernel->getContainer();
$this->service = $this->container->get('geolocation.interpreter');
$this->citiesMap = $this->container->getParameter("citiesmap");
self::tearDown();
}
/**
* @dataProvider locationsProvider
*/
public function testCompanyCityFromCity($location, $expected)
{
$city = $this->service->getCompanyCityFromCity($location);
$this->assertEquals($expected, $city);
}
public function locationsProvider()
{
$return = array();
foreach ($this->citiesMap as $area) {
$return[] = [
$area['external_service_area'],
$area['company_area']
];
}
return $return;
}
}
Run Code Online (Sandbox Code Playgroud)
为foreach()提供了无效的参数
如果我手动写的返回locationsProvider()它的作品
return [
["Barcelona", "Barcelona"],
["Madrid", "Madrid"],
["Cartagena", "Murcia"]
];
Run Code Online (Sandbox Code Playgroud)
我也检查了foreach setUp(),它返回正确的预期数组。
看来@dataProvider是执行之前的setUp()方法。
有其他方法可以做到这一点吗?
dataProvider方法内获取所有数据(包括服务对象)TL&DR这应该可以做到:
class InterpreterServiceTest extends KernelTestCase
{
/**
* @dataProvider locationsProvider
*/
public function testCompanyCityFromCity($service, $location, $expected)
{
$city = $service->getCompanyCityFromCity($location);
$this->assertEquals($expected, $city);
}
public function locationsProvider()
{
self::bootKernel();
$container = self::$kernel->getContainer();
$service = $this->container->get('geolocation.interpreter');
$citiesMap = $this->container->getParameter("citiesmap");
// self::tearDown(); - depends on what is in the tearDown
$return = array();
foreach ($citiesMap as $area) {
$return[] = [
$service,
$area['external_service_area'],
$area['company_area']
];
}
return $return;
}
}
Run Code Online (Sandbox Code Playgroud)
为什么:
setUp和方法都在类的方法setUpBeforeClass内部运行。然而,来自 的数据是作为函数的一部分提前计算的。runPHPUnit_Framework_TestSuitedataProvidercreateTest
| 归档时间: |
|
| 查看次数: |
1674 次 |
| 最近记录: |