如何测试传递给方法构造的对象的参数顺序

How to test the order of parameters passed to an object constructed by a method

我正在使用一种方法测试一个简单的工厂 class,其中 returns 一个 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 测试订单,但我已经超越了仅测试此单元的范围。但是我确实觉得应该测试订单,因为如果我更改它们,测试仍然会通过。

您正在测试的方法是一个工厂。它创建一个对象。如果确保它的预期类型对你来说还不够,你需要验证它的状态。用 getter 检查它,或者创建一个你期望接收的对象并使用 assertEquals() 来比较它。