【发布时间】:2012-12-07 09:14:18
【问题描述】:
断言:
$chain->expects($this->once())
->method('addMethodCall')
->with(
'addOptionsProvider',
array(
$this->isInstanceOf('Symfony\Component\DependencyInjection\Reference'),
$this->equalTo(7)
)
);
$chain 实际上是Definition 的模拟对象,这是我要测试的代码:
$definition->addMethodCall(
'addOptionsProvider',
array(new Reference($id), $priority)
);
我正在开始 PHPUnit,所以我真的不知道我错过了什么。我发现断言论点真的很难理解。我已经包含了一个图像,其中包含断言和实际参数之间的视觉差异。
PHPUnit_Framework_ExpectationFailedException : 期望失败 方法名称等于调用 1 次时 调用参数 1 Symfony\Component\DependencyInjection\Definition::addMethodCall('addOptionsProvider', 数组 (...)) 与预期值不匹配。
编辑:实际上,我最终得到了这个:
$chain->expects($this->once())
->method('addMethodCall')
->with(
$this->identicalTo('addOptionsProvider'),
$this->logicalAnd(
$this->isType('array'),
$this->arrayHasKey(0),
$this->arrayHasKey(1)
)
);
但我不能“进入”数组值进行进一步断言!
【问题讨论】:
-
测试难度是其编码方式的产物。这是一个为什么应该使用 TDD 的例子,否则你有你不能轻易测试的功能代码(这通常意味着它也不是很可重用)。 new Reference($id) 应该被模拟,或者如果一个方法被用来检索那个实例,在你的测试中对它的期望。
标签: php unit-testing testing phpunit