Laravel 使用 PHPUnit 测试断言正确的顺序
Laravel Testing with PHPUnit assert correct order
我定义了一个范围,它按 published_at 时间戳对我的实体进行排序。我想写一个测试来确保正确的顺序。
但是我不知何故找不到 "nice" 方法来做到这一点。
$novels = Novel::published()->get();
这是我最好的猜测
for($i = 1; $i < count($novels); $i++)
{
$this->assertGreaterThanOrEqual(
$novels->toArray()[$i - 1]['published_at'],
$novels->toArray()[$i]['published_at']
);
}
谁能提供更好的方法?
如果有效的话,你在测试中所做的没有任何问题。
也就是说,如果您想缩短测试时间,您可以这样做:
$this->assertEquals($novels->pluck('id'), $novels->sortBy('published_at')->pluck('id'),
'The Novels are not being ordered by published_at date');
希望对您有所帮助!
我定义了一个范围,它按 published_at 时间戳对我的实体进行排序。我想写一个测试来确保正确的顺序。
但是我不知何故找不到 "nice" 方法来做到这一点。
$novels = Novel::published()->get();
这是我最好的猜测
for($i = 1; $i < count($novels); $i++)
{
$this->assertGreaterThanOrEqual(
$novels->toArray()[$i - 1]['published_at'],
$novels->toArray()[$i]['published_at']
);
}
谁能提供更好的方法?
如果有效的话,你在测试中所做的没有任何问题。
也就是说,如果您想缩短测试时间,您可以这样做:
$this->assertEquals($novels->pluck('id'), $novels->sortBy('published_at')->pluck('id'),
'The Novels are not being ordered by published_at date');
希望对您有所帮助!