我已经定义了一个范围,该范围按stamped_at时间戳对我的实体进行排序。我想编写一个测试以确保正确的订购。
但是我以某种方式未能找到一种“不错”的方式来做到这一点。
$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');
希望这可以帮助!