【问题标题】:Laravel Testing with PHPUnit assert correct order使用 PHPUnit 进行 Laravel 测试断言正确的顺序
【发布时间】:2017-03-13 15:28:45
【问题描述】:

我已经定义了一个范围,该范围通过发布的时间戳对我的实体进行排序。我想写一个测试来确保正确的排序。

但是我不知何故未能找到一种“好”的方法来做到这一点。

$novels = Novel::published()->get();

这是我最好的猜测

for($i = 1; $i < count($novels); $i++) 
{
    $this->assertGreaterThanOrEqual(
        $novels->toArray()[$i - 1]['published_at'], 
        $novels->toArray()[$i]['published_at']
    );
}

谁能提供更好的方法?

【问题讨论】:

    标签: php laravel testing phpunit


    【解决方案1】:

    如果测试有效,则说明您在测试中所做的事情没有任何问题。

    话虽如此,如果您想缩短测试时间,可以执行以下操作:

    $this->assertEquals($novels->pluck('id'), $novels->sortBy('published_at')->pluck('id'),
            'The Novels are not being ordered by published_at date');
    

    希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 2016-06-04
      • 2015-09-01
      • 2015-08-25
      • 2015-03-03
      • 1970-01-01
      • 2020-04-26
      • 2017-04-12
      • 2014-05-15
      • 2013-09-13
      相关资源
      最近更新 更多