mercredi 8 juin 2016

Laravel 5.2 - Invalid argument supplied for foreach() - using phpunit

I get the above error when testing my application with phpunit command.

public function testProductCreationFailsWhenNameNotProvided()
{
    $product = factory(\App\Product::class)->make(['name' => '']);

    $this->post(route('api.products.store'), $product->jsonSerialize())
        ->seeJson(['name' => ['The name field is required.']]) /*line 86*/
        ->assertResponseStatus(422);
}

The full error report is here:

There was 1 error:
1) ExampleTest::testProductCreationFailsWhenNameNotProvided
ErrorException: Invalid argument supplied for foreach()
C:\xampp\htdocs\product-  service\vendor\laravel\framework\src\Illuminate\Support\Arr.php:494
C:\xampp\htdocs\product-service\vendor\laravel\framework\src\Illuminate\Foundation\Testing\Concerns\MakesHttpRequests.php:231
C:\xampp\htdocs\product-service\vendor\laravel\framework\src\Illuminate\Foundation\Testing\Concerns\MakesHttpRequests.php:257
C:\xampp\htdocs\product-service\tests\ExampleTest.php:86
C:\xampp\php\pear\PHPUnit\TextUI\Command.php:176
C:\xampp\php\pear\PHPUnit\TextUI\Command.php:129
FAILURES!
Tests: 7, Assertions: 43, Errors: 1.

I confess that this code is not originally mine - it is copied from a Laravel tutorial. It worked fine there. Unfortunately the answer to this related question did not help me either. Laravel 5.1 + PHPunit - API test returns always invalid argument error foreach

Aucun commentaire:

Enregistrer un commentaire