【发布时间】:2017-01-31 01:47:35
【问题描述】:
我正在使用一个返回 TagModel 的方法测试一个简单的工厂类。
class TagFactory
{
public function buildFromArray(array $tagData)
{
return new TagModel(
$tagData['t_id'],
$tagData['t_promotion_id'],
$tagData['t_type_id'],
$tagData['t_value']
);
}
}
我可以测试这个方法……
public function testbuildFromArray()
{
$tagData = [
't_id' => 1,
't_promotion_id' => 2,
't_type_id' => 3,
't_value' => 'You are valued',
];
$tagFactory = new TagFactory();
$result = $tagFactory->buildFromArray($tagData);
$this->assertInstanceOf(TagModel::class, $result);
}
如果我更改new TagModel… 中的参数顺序,测试仍然会通过。
如果我预言TagModel...
$tagModel = $this->prophesize(TagModel::class);
$tagModel->willBeConstructedWith(
[
$tagData['t_id'],
$tagData['t_promotion_id'],
$tagData['t_type_id'],
$tagData['t_value']
]
);
...但是我应该断言什么呢? assertSame 不起作用,因为它们不是。
我可以使用来自TagModel 的 getter 来测试订单,但是我已经超越了仅测试这个单元。但我确实觉得应该测试订单,因为如果我更改它们,测试仍然通过。
【问题讨论】: