Question

I wrote a test to pull 10 articles from my database. I then wrote the code to make the test pass and it works. However, now I want to write a test to make sure the 10 articles I pull from the database are in descending order based on created_at. I'm not sure how to go about this, at all.

Test Method:

public function testPullTenArticles()
{
    for ($i = 0; $i < 20; $i++)
    {
        $tempArticle = new Article;
        $tempArticle->save();
    }

    $this->assertEquals(10, count($this->article->getArticles(10)));
}

Real Method:

public function getArticles($count)
{
    $articles = Article::take($count)->get();
    return $articles;
}
Was it helpful?

Solution

If you're using the orderBy clause to retrieve your objects, then you shouldn't need to test it - the good folks who wrote the Laravel & Eloquent code have already done that.

You can order your collection like this:

$articles = Article::orderBy("created_at", "desc")->take($count)->get();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top