mardi 26 mars 2019

phpunit: Unable to assertCount efficiently on laravel feature testing

When I try to assertCount() I get this error:

PHPUnit\Framework\Exception: Argument #2 (No Value) of PHPUnit\Framework\Assert::assertCount() must be a countable or traversable

which means I'm not passing something that is countable.

Here is my Controller, I'm simply returning the collection.

 public function index()
    {
        // Get articles
        $articles = Article::all();

        return $articles->toArray();
}

Here, I can do count($articles) and it will give me the count but phpunit is converting this collection of data into another object. Hence, which gives me an error.

i.e

/** @test */
    public function a_user_can_view_articles(){

        factory('App\Article')->create();

        $response = $this->get('api/articles');

        $this->assertCount(1, $response);

    }

But I if assertCount it like:

$this->assertCount(1, $response->baseResponse->data);

it works. I don't wanna do it all the time. Is there anyway i can do it in a better manner



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2HFkw7n
via IFTTT

Aucun commentaire:

Enregistrer un commentaire